Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/to string"

From cppreference.com
< cpp‎ | string‎ | basic string
m (seealso to_chars)
m (Example: +the aforementioned initializer_list header <_<;)
 
(27 intermediate revisions by 15 users not shown)
Line 2: Line 2:
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | string}}
+
{{dcl header|string}}
{{dcl | num=1 | since=c++11 |
+
{{dcl|num=1|since=c++11|
 
std::string to_string( int value );
 
std::string to_string( int value );
 
}}
 
}}
{{dcl | num=2 | since=c++11 |
+
{{dcl|num=2|since=c++11|
 
std::string to_string( long value );
 
std::string to_string( long value );
 
}}
 
}}
{{dcl | num=3 | since=c++11 |
+
{{dcl|num=3|since=c++11|
 
std::string to_string( long long value );
 
std::string to_string( long long value );
 
}}
 
}}
{{dcl | num=4 | since=c++11 |
+
{{dcl|num=4|since=c++11|
 
std::string to_string( unsigned value );
 
std::string to_string( unsigned value );
 
}}
 
}}
{{dcl | num=5 | since=c++11 |
+
{{dcl|num=5|since=c++11|
 
std::string to_string( unsigned long value );
 
std::string to_string( unsigned long value );
 
}}
 
}}
{{dcl | num=6 | since=c++11 |
+
{{dcl|num=6|since=c++11|
 
std::string to_string( unsigned long long value );
 
std::string to_string( unsigned long long value );
 
}}
 
}}
{{dcl | num=7 | since=c++11 |
+
{{dcl|num=7|since=c++11|
 
std::string to_string( float value );
 
std::string to_string( float value );
 
}}
 
}}
{{dcl | num=8 | since=c++11 |
+
{{dcl|num=8|since=c++11|
 
std::string to_string( double value );
 
std::string to_string( double value );
 
}}
 
}}
{{dcl | num=9 | since=c++11 |
+
{{dcl|num=9|since=c++11|
 
std::string to_string( long double value );
 
std::string to_string( long double value );
 
}}
 
}}
Line 34: Line 34:
 
Converts a numeric value to {{lc|std::string}}.
 
Converts a numeric value to {{lc|std::string}}.
  
@1@ Converts a signed decimal integer to a string with the same content as what  {{c|std::sprintf(buf, "%d", value)}} would produce for sufficiently large {{tt|buf}}.
+
{{rrev multi|until1=c++26
@2@ Converts a signed decimal integer to a string with the same content as what  {{c|std::sprintf(buf, "%ld", value)}} would produce for sufficiently large {{tt|buf}}.
+
|rev1=
@3@ Converts a signed decimal integer to a string with the same content as what  {{c|std::sprintf(buf, "%lld", value)}} would produce for sufficiently large {{tt|buf}}.
+
Let {{tti|buf}} be an internal to the conversion functions buffer, sufficiently large to contain the result of conversion.
@4@ Converts an unsigned decimal integer to a string with the same content as what  {{c|std::sprintf(buf, "%u", value)}} would produce for sufficiently large {{tt|buf}}.
+
 
@5@ Converts an unsigned decimal integer to a string with the same content as what  {{c|std::sprintf(buf, "%lu", value)}} would produce for sufficiently large {{tt|buf}}.
+
@1@ Converts a signed integer to a string as if by {{c|std::sprintf(buf, "%d", value)}}.
@6@ Converts an unsigned decimal integer to a string with the same content as what  {{c|std::sprintf(buf, "%llu", value)}} would produce for sufficiently large {{tt|buf}}.
+
@2@ Converts a signed integer to a string as if by {{c|std::sprintf(buf, "%ld", value)}}.
@7,8@ Converts a floating point value to a string with the same content as what  {{c|std::sprintf(buf, "%f", value)}} would produce for sufficiently large {{tt|buf}}.
+
@3@ Converts a signed integer to a string as if by {{c|std::sprintf(buf, "%lld", value)}}.
@9@ Converts a floating point value to a string with the same content as what  {{c|std::sprintf(buf, "%Lf", value)}} would produce for sufficiently large {{tt|buf}}.
+
@4@ Converts an unsigned integer to a string as if by {{c|std::sprintf(buf, "%u", value)}}.
 +
@5@ Converts an unsigned integer to a string as if by {{c|std::sprintf(buf, "%lu", value)}}.
 +
@6@ Converts an unsigned integer to a string as if by {{c|std::sprintf(buf, "%llu", value)}}.
 +
@7,8@ Converts a floating point value to a string as if by {{c|std::sprintf(buf, "%f", value)}}.
 +
@9@ Converts a floating point value to a string as if by {{c|std::sprintf(buf, "%Lf", value)}}.
 +
|rev2=
 +
@1-9@ Converts a numeric value to a string as if by {{c|std::format("{}", value)}}.
 +
}}
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | value | a numeric value to convert}}
+
{{par|value|a numeric value to convert}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
a string holding the converted value
+
A string holding the converted value.
  
===Notes===
+
===Exceptions===
 +
May throw {{lc|std::bad_alloc}} from the {{lc|std::string}} constructor.
  
 +
===Notes===
 
* With floating point types {{tt|std::to_string}} may yield unexpected results as the number of significant digits in the returned string can be zero, see the example.
 
* With floating point types {{tt|std::to_string}} may yield unexpected results as the number of significant digits in the returned string can be zero, see the example.
 
* The return value may differ significantly from what {{tt|std::cout}} prints by default, see the example.
 
* The return value may differ significantly from what {{tt|std::cout}} prints by default, see the example.
 +
{{rrev|until=c++26|
 +
* {{tt|std::to_string}} relies on the current C locale for formatting purposes, and therefore concurrent calls to {{tt|std::to_string}} from multiple threads may result in partial serialization of calls.
 +
** The results of overloads for integer types do not rely on the current C locale, and thus implementations generally avoid access to the current C locale in these overloads for both correctness and performance. However, such avoidance is not guaranteed by the standard.
 +
}}
 +
 +
C++17 provides {{ltt|cpp/utility/to_chars|std::to_chars}} as a higher-performance locale-independent alternative.
 +
 +
{{feature test macro|__cpp_lib_to_string|Redefining {{tt|std::to_string}} in terms of {{lc|std::format}}|std=C++26|value=202306L}}
  
 
===Example===
 
===Example===
{{example|
+
{{example
| code=  
+
|code=
 +
#include <cstdio>
 +
#include <format>
 +
#include <initializer_list>
 
#include <iostream>
 
#include <iostream>
 
#include <string>
 
#include <string>
+
 
int main()  
+
#if __cpp_lib_to_string >= 202306L
 +
constexpr auto revision() { return " (post C++26)"; }
 +
#else
 +
constexpr auto revision() { return " (pre C++26)"; }
 +
#endif
 +
 
 +
int main()
 
{
 
{
     double f = 23.43;
+
     for (const double f : {1.23456789555555, 23.43, 1e-9, 1e40, 1e-40, 123456789.0})
    double f2 = 1e-9;
+
     {
    double f3 = 1e40;
+
        std::cout << "to_string:\t" << std::to_string(f) << revision() << '\n';
    double f4 = 1e-40;
+
 
     double f5 = 123456789;
+
        // Before C++26, the output of std::to_string matches std::printf.
    std::string f_str = std::to_string(f);
+
        std::printf("printf:\t\t%f\n", f);
    std::string f_str2 = std::to_string(f2); // Note: returns "0.000000"
+
 
    std::string f_str3 = std::to_string(f3); // Note: Does not return "1e+40".
+
        // As of C++26, the output of std::to_string matches std::format.
    std::string f_str4 = std::to_string(f4); // Note: returns "0.000000"
+
        std::cout << std::format("format:\t\t{}\n", f);
    std::string f_str5 = std::to_string(f5);
+
 
    std::cout << "std::cout: " << f << '\n'
+
        std::cout << "std::cout:\t" << f << "\n\n";
              << "to_string: " << f_str  << "\n\n"
+
    }
              << "std::cout: " << f2 << '\n'
+
              << "to_string: " << f_str2 << "\n\n"
+
              << "std::cout: " << f3 << '\n'
+
              << "to_string: " << f_str3 << "\n\n"
+
              << "std::cout: " << f4 << '\n'
+
              << "to_string: " << f_str4 << "\n\n"
+
              << "std::cout: " << f5 << '\n'
+
              << "to_string: " << f_str5 << '\n';
+
 
}
 
}
| output=
+
|p=true
std::cout: 23.43
+
|output=
to_string: 23.430000
+
to_string:      1.234568 (pre C++26)
 +
printf:        1.234568
 +
format:        1.23456789555555
 +
std::cout:     1.23457
 +
 
 +
to_string:     23.430000 (pre C++26)
 +
printf:        23.430000
 +
format:        23.43
 +
std::cout:      23.43
  
std::cout: 1e-09
+
to_string:     0.000000 (pre C++26)
to_string: 0.000000
+
printf:         0.000000
 +
format:         1e-09
 +
std::cout:      1e-09
  
std::cout: 1e+40
+
to_string:     10000000000000000303786028427003666890752.000000 (pre C++26)
to_string: 10000000000000000303786028427003666890752.000000
+
printf:         10000000000000000303786028427003666890752.000000
 +
format:         1e+40
 +
std::cout:      1e+40
  
std::cout: 1e-40
+
to_string:     0.000000 (pre C++26)
to_string: 0.000000
+
printf:         0.000000
 +
format:         1e-40
 +
std::cout:      1e-40
  
std::cout: 1.23457e+08
+
to_string:      123456789.000000 (pre C++26)
to_string: 123456789.000000
+
printf:        123456789.000000
 +
format:        123456789
 +
std::cout:     1.23457e+08
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string/dsc to_wstring}}
+
{{dsc inc|cpp/string/basic_string/dsc to_wstring}}
{{dsc inc | cpp/string/basic_string/dsc stoul}}
+
{{dsc inc|cpp/string/basic_string/dsc stoul}}
{{dsc inc | cpp/string/basic_string/dsc stol}}
+
{{dsc inc|cpp/string/basic_string/dsc stol}}
{{dsc inc | cpp/string/basic_string/dsc stof}}
+
{{dsc inc|cpp/string/basic_string/dsc stof}}
{{dsc inc | cpp/utility/dsc to_chars}}  
+
{{dsc inc|cpp/utility/dsc to_chars}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/basic string/to string]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/string/basic string/to string]]
+
[[fr:cpp/string/basic string/to string]]
+
[[it:cpp/string/basic string/to string]]
+
[[ja:cpp/string/basic string/to string]]
+
[[pt:cpp/string/basic string/to string]]
+
[[ru:cpp/string/basic string/to string]]
+
[[zh:cpp/string/basic string/to string]]
+

Latest revision as of 12:29, 16 May 2024

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
to_string
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
Defined in header <string>
std::string to_string( int value );
(1) (since C++11)
std::string to_string( long value );
(2) (since C++11)
std::string to_string( long long value );
(3) (since C++11)
std::string to_string( unsigned value );
(4) (since C++11)
std::string to_string( unsigned long value );
(5) (since C++11)
std::string to_string( unsigned long long value );
(6) (since C++11)
std::string to_string( float value );
(7) (since C++11)
std::string to_string( double value );
(8) (since C++11)
std::string to_string( long double value );
(9) (since C++11)

Converts a numeric value to std::string.

Let buf be an internal to the conversion functions buffer, sufficiently large to contain the result of conversion.

1) Converts a signed integer to a string as if by std::sprintf(buf, "%d", value).
2) Converts a signed integer to a string as if by std::sprintf(buf, "%ld", value).
3) Converts a signed integer to a string as if by std::sprintf(buf, "%lld", value).
4) Converts an unsigned integer to a string as if by std::sprintf(buf, "%u", value).
5) Converts an unsigned integer to a string as if by std::sprintf(buf, "%lu", value).
6) Converts an unsigned integer to a string as if by std::sprintf(buf, "%llu", value).
7,8) Converts a floating point value to a string as if by std::sprintf(buf, "%f", value).
9) Converts a floating point value to a string as if by std::sprintf(buf, "%Lf", value).
(until C++26)
1-9) Converts a numeric value to a string as if by std::format("{}", value).
(since C++26)

Contents

[edit] Parameters

value - a numeric value to convert

[edit] Return value

A string holding the converted value.

[edit] Exceptions

May throw std::bad_alloc from the std::string constructor.

[edit] Notes

  • With floating point types std::to_string may yield unexpected results as the number of significant digits in the returned string can be zero, see the example.
  • The return value may differ significantly from what std::cout prints by default, see the example.
  • std::to_string relies on the current C locale for formatting purposes, and therefore concurrent calls to std::to_string from multiple threads may result in partial serialization of calls.
    • The results of overloads for integer types do not rely on the current C locale, and thus implementations generally avoid access to the current C locale in these overloads for both correctness and performance. However, such avoidance is not guaranteed by the standard.
(until C++26)

C++17 provides std::to_chars as a higher-performance locale-independent alternative.

Feature-test macro Value Std Feature
__cpp_lib_to_string 202306L (C++26) Redefining std::to_string in terms of std::format

[edit] 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";
    }
}

Possible output:

to_string:      1.234568 (pre C++26)
printf:         1.234568
format:         1.23456789555555
std::cout:      1.23457
 
to_string:      23.430000 (pre C++26)
printf:         23.430000
format:         23.43
std::cout:      23.43
 
to_string:      0.000000 (pre C++26)
printf:         0.000000
format:         1e-09
std::cout:      1e-09
 
to_string:      10000000000000000303786028427003666890752.000000 (pre C++26)
printf:         10000000000000000303786028427003666890752.000000
format:         1e+40
std::cout:      1e+40
 
to_string:      0.000000 (pre C++26)
printf:         0.000000
format:         1e-40
std::cout:      1e-40
 
to_string:      123456789.000000 (pre C++26)
printf:         123456789.000000
format:         123456789
std::cout:      1.23457e+08

[edit] See also

converts an integral or floating-point value to wstring
(function) [edit]
(C++11)(C++11)
converts a string to an unsigned integer
(function) [edit]
(C++11)(C++11)(C++11)
converts a string to a signed integer
(function) [edit]
(C++11)(C++11)(C++11)
converts a string to a floating point value
(function) [edit]
(C++17)
converts an integer or floating-point value to a character sequence
(function) [edit]