From: Rene Gassmoeller Date: Mon, 30 Nov 2015 23:24:42 +0000 (-0600) Subject: Add a Utilities::number_to_string function X-Git-Tag: v8.4.0-rc2~166^2~4 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c7bd8002397b5c45b48cc307c070a129f10f66f;p=dealii.git Add a Utilities::number_to_string function --- diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 1ac3b2ec70..8ba7baf461 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -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 itoa() 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 + * Utilities::to_string() instead. In its current implementation + * the function simply calls to_string(). */ 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 lexical_cast() had been called. + */ + template + 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. diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 9de4a062c6..1e07737018 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -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 + std::string + to_string (const number value, const unsigned int digits) { std::string lc_string = boost::lexical_cast(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, unsigned int); + template std::string to_string (long int, unsigned int); + template std::string to_string (long long int, unsigned int); + template std::string to_string (unsigned int, unsigned int); + template std::string to_string (unsigned long int, unsigned int); + template std::string to_string (unsigned long long int, unsigned int); + template std::string to_string (float, unsigned int); + template std::string to_string (double, unsigned int); + template std::string to_string (long double, unsigned int); + } DEAL_II_NAMESPACE_CLOSE