]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Reintroduce boost::lexical_cast for everything but integers. 9410/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 23 Jan 2020 00:06:06 +0000 (17:06 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Thu, 23 Jan 2020 00:06:24 +0000 (17:06 -0700)
source/base/utilities.cc

index 263993fba75e1250d9cf5cc3e9187cf662ad4918..e7a1d7009a7a1a5c29ba02eb0a5aa89e892ccf68 100644 (file)
@@ -437,11 +437,20 @@ namespace Utilities
   std::string
   to_string(const number value, const unsigned int digits)
   {
-    std::string lc_string = std::to_string(value);
-
-    if (digits == numbers::invalid_unsigned_int)
-      return lc_string;
-    else if (lc_string.size() < digits)
+    // For integer data types, use the standard std::to_string()
+    // function. On the other hand, that function is defined in terms
+    // of std::sprintf, which does not use the usual std::iostream
+    // interface and tries to render floating point numbers in awkward
+    // ways (see
+    // https://en.cppreference.com/w/cpp/string/basic_string/to_string). So
+    // resort to boost::lexical_cast for all other types (in
+    // particular for floating point types.
+    std::string lc_string = (std::is_integral<number>::value ?
+                               std::to_string(value) :
+                               boost::lexical_cast<std::string>(value));
+
+    if ((digits != numbers::invalid_unsigned_int) &&
+        (lc_string.size() < digits))
       {
         // We have to add the padding zeroes in front of the number
         const unsigned int padding_position = (lc_string[0] == '-') ? 1 : 0;
@@ -449,10 +458,12 @@ namespace Utilities
         const std::string padding(digits - lc_string.size(), '0');
         lc_string.insert(padding_position, padding);
       }
+
     return lc_string;
   }
 
 
+
   std::string
   replace_in_string(const std::string &input,
                     const std::string &from,

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.