Difference between revisions of "cpp/string/basic string/to string"
From cppreference.com
< cpp | string | basic string
(+summary) |
|||
Line 35: | Line 35: | ||
@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}}. | @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}}. | ||
− | @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}} | + | @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}}. |
@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}}. | @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}}. | ||
@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}}. | @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}}. |
Revision as of 07:00, 5 June 2014
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.
1) Converts a signed decimal integer to a string with the same content as what std::sprintf(buf, "%d", value) would produce for sufficiently large
buf
.2) Converts a signed decimal integer to a string with the same content as what std::sprintf(buf, "%ld", value) would produce for sufficiently large
buf
.3) Converts a signed decimal integer to a string with the same content as what std::sprintf(buf, "%lld", value) would produce for sufficiently large
buf
.4) Converts an unsigned decimal integer to a string with the same content as what std::sprintf(buf, "%u", value) would produce for sufficiently large
buf
.5) Converts an unsigned decimal integer to a string with the same content as what std::sprintf(buf, "%lu", value) would produce for sufficiently large
buf
.6) Converts an unsigned decimal integer to a string with the same content as what std::sprintf(buf, "%llu", value) would produce for sufficiently large
buf
.7,8) Converts a floating point value to a string with the same content as what std::sprintf(buf, "%f", value) would produce for sufficiently large
buf
.9) Converts a floating point value to a string with the same content as what std::sprintf(buf, "%Lf", value) would produce for sufficiently large
buf
.Contents |
Parameters
value | - | a numeric value to convert |
Return value
a string holding the converted value
Example
Run this code
#include <iostream> #include <string> int main() { double f = 23.43; std::string f_str = std::to_string(f); std::cout << f_str << '\n'; }
Output:
23.430000
See also
(C++11) |
converts an integral or floating-point value to wstring (function) |
(C++11)(C++11) |
converts a string to an unsigned integer (function) |
(C++11)(C++11)(C++11) |
converts a string to a signed integer (function) |
(C++11)(C++11)(C++11) |
converts a string to a floating point value (function) |