ctl::to_string()
is a header-only C++ template function corresponding to C++11's std::to_string()
plus format speicifier and precision speicifer features.
As C++20 introduced std::format
, demand for this kind of library may no longer exist. But ctl::json contains this library, so I decided to release a standalone version, too.
The oldest compiler on where I confirm this library is available is Visual C++ 2005 in Visual Studio 2005.
Place ctlstring.hpp
somewhere in your PATH and include it.
Then call ctl::to_string<AnyStringType>(Number, a2, a3)
where the first argument Number
is a value to be converted to a string and the template argument AnyStringType
is a string type that should be used as the return type, i.e., the type of the string converted from the number value.
The second and third arguments (a2
and a3
) are optional, and their meanings change depending on the type of the first argument (integer or floating-point type).
When a value of an integer type is specified to convert to a string as the first argument, the second and third arguments are interpreted as follows:
A string type specified as the first template argument is used as the return type of the function.
// When 1st argument is a value of any integer type. template <typename stringT, typename T> stringT to_string(T value, int base = 10, int precision = 1); // Example. std::string s = ctl::to_string<std::string>(123, -16, 4); // std::string("007B") is returned.
If the second parameter base
is a value from 11 to 36 inclusive, lowercase letters 'a' to 'z' are used for representing an 10 or more number.
If base
is a negative value in the range -11~-36,
its absolute value is used in conversion and uppercase letters are used instead of lowercase letters
(For example, when -16 is specified, a number is stringified in the base 16 system that uses the uppercase letters "ABCDEF" for 10~15).
If the places of a stringified number is fewer than the value specified by precision
, 0
padding is performed.
When a value of a floating-point type is specified to convert to a string as the first argument, the second and third arguments are interpreted as follows:
'e'
/'E'
, 'f'
/'F'
, 'g'
/'G'
'a'
/'A'
are available. Thier meanings are the same as the printf()
family's "%e"
/"%E"
, "%f"
/"%F"
, "%g"
/"%G"
, "%a"
/"%A"
, respectively.
When omitted, 'f'
is used.
In short, given the printf()
family's format string "%.PT'
, a number value P
(precision) is specified by the third parameter, and a character literal value T
(type) is specified by the second parameter.
As above, the return type can be specified by the first template argument.
// When 1st argument is a value of any floating-point type.
template <typename stringT>
stringT to_string(double val, const int fmt = 'f', const int precision = 6);
template <typename stringT>
stringT to_string(float val, const int fmt = 'f', const int precision = 6);
template <typename stringT>
stringT to_string(long double val, const int fmt = 'f', const int precision = 6);
When a negative value is specified as the third argument precision
, its absolute value is used in conversion and the trailing zeros after the decimal point are removed. If no digits are left after the decimal point, the decimal point itself is also removed.
The default namespace
of this library is ctl
, but you can change it with any name you like by defining NAMESPACE_CTLSTRING
in advance, like #define NAMESPACE_CTLSTRING othername
.