]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a Utilities::number_to_string function
authorRene Gassmoeller <R.Gassmoeller@mailbox.org>
Mon, 30 Nov 2015 23:24:42 +0000 (17:24 -0600)
committerRene Gassmoeller <R.Gassmoeller@mailbox.org>
Thu, 10 Dec 2015 03:01:35 +0000 (21:01 -0600)
include/deal.II/base/utilities.h
source/base/utilities.cc

index 1ac3b2ec703bb267c3f2f12e5cd6200dc3b854ee..8ba7baf461fa8e109e62a553ddaac05932ecf057 100644 (file)
@@ -50,17 +50,37 @@ namespace Utilities
 {
 
   /**
-   * Convert a number @p i to a string, with as many digits as given to fill
+   * Convert a number @p value to a string, with as many digits as given to fill
    * with leading zeros.
    *
    * If the second parameter is left at its default value, the number is not
-   * padded with leading zeros. The result is then the same as of the standard
+   * padded with leading zeros. The result is then the same as if the standard
    * C function <code>itoa()</code> had been called.
+   *
+   * When calling this function signed integers are implicitly converted to
+   * unsigned integers and long integers might experience an overflow.
+   *
+   * @note The use of this function is discouraged and users should use
+   * <code>Utilities::to_string()</code> instead. In its current implementation
+   * the function simply calls <code>to_string<unsigned int>()</code>.
    */
   std::string
-  int_to_string (const unsigned int i,
+  int_to_string (const unsigned int value,
                  const unsigned int digits = numbers::invalid_unsigned_int);
 
+  /**
+   * Convert a number @p value to a string, with as many digits as given to fill
+   * with leading zeros.
+   *
+   * If the second parameter is left at its default value, the number is not
+   * padded with leading zeros. The result is then the same as if the boost
+   * function <code>lexical_cast<std::string>()</code> had been called.
+   */
+  template <typename number>
+  std::string
+  to_string (const number value,
+             const unsigned int digits = numbers::invalid_unsigned_int);
+
   /**
    * Determine how many digits are needed to represent numbers at most as
    * large as the given number.
index 9de4a062c68ed08d96283c695f20a215bed4482a..1e077370180807755aaf6f6c234b026364e86af4 100644 (file)
@@ -80,6 +80,13 @@ namespace Utilities
 
   std::string
   int_to_string (const unsigned int value, const unsigned int digits)
+  {
+    return to_string(value,digits);
+  }
+
+  template <typename number>
+  std::string
+  to_string (const number value, const unsigned int digits)
   {
     std::string lc_string = boost::lexical_cast<std::string>(value);
 
@@ -87,8 +94,15 @@ namespace Utilities
       return lc_string;
     else if (lc_string.size() < digits)
       {
-        std::string padding(digits - lc_string.size(), '0');
-        lc_string.insert(0, padding);
+        // We have to add the padding zeroes in front of the number
+        const unsigned int padding_position = (lc_string[0] == '-')
+                                              ?
+                                              1
+                                              :
+                                              0;
+
+        const std::string padding(digits - lc_string.size(), '0');
+        lc_string.insert(padding_position, padding);
       }
     return lc_string;
   }
@@ -820,6 +834,16 @@ namespace Utilities
 
 #endif
 
+  template std::string to_string<int> (int, unsigned int);
+  template std::string to_string<long int> (long int, unsigned int);
+  template std::string to_string<long long int> (long long int, unsigned int);
+  template std::string to_string<unsigned int> (unsigned int, unsigned int);
+  template std::string to_string<unsigned long int> (unsigned long int, unsigned int);
+  template std::string to_string<unsigned long long int> (unsigned long long int, unsigned int);
+  template std::string to_string<float> (float, unsigned int);
+  template std::string to_string<double> (double, unsigned int);
+  template std::string to_string<long double> (long double, unsigned int);
+
 }
 
 DEAL_II_NAMESPACE_CLOSE

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.