std::to_string
Min standard notice:
Header: <string>
Converts a numeric value to std::string.
# Declarations
std::string to_string( int value );
(since C++11)
std::string to_string( long value );
(since C++11)
std::string to_string( long long value );
(since C++11)
std::string to_string( unsigned value );
(since C++11)
std::string to_string( unsigned long value );
(since C++11)
std::string to_string( unsigned long long value );
(since C++11)
std::string to_string( float value );
(since C++11)
std::string to_string( double value );
(since C++11)
std::string to_string( long double value );
(since C++11)
# Parameters
value: a numeric value to convert
# Return value
A string holding the converted value.
# Notes
C++17 provides std::to_chars as a higher-performance locale-independent alternative.
# Example
#include <cstdio>
#include <format>
#include <initializer_list>
#include <iostream>
#include <string>
#if __cpp_lib_to_string >= 202306L
constexpr auto revision() { return " (post C++26)"; }
#else
constexpr auto revision() { return " (pre C++26)"; }
#endif
int main()
{
for (const double f : {1.23456789555555, 23.43, 1e-9, 1e40, 1e-40, 123456789.0})
{
std::cout << "to_string:\t" << std::to_string(f) << revision() << '\n';
// Before C++26, the output of std::to_string matches std::printf.
std::printf("printf:\t\t%f\n", f);
// As of C++26, the output of std::to_string matches std::format.
std::cout << std::format("format:\t\t{}\n", f);
std::cout << "std::cout:\t" << f << "\n\n";
}
}