From: Wolfgang Bangerth Date: Thu, 23 Jan 2020 00:06:06 +0000 (-0700) Subject: Reintroduce boost::lexical_cast for everything but integers. X-Git-Tag: v9.2.0-rc1~614^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0264f178c505c14581acd7fc673259d839ab74ff;p=dealii.git Reintroduce boost::lexical_cast for everything but integers. --- diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 263993fba7..e7a1d7009a 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -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::value ? + std::to_string(value) : + boost::lexical_cast(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,