Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
(%f is for float and %lf is for double.)
(Undo revision 132015 by 111.221.205.74 (talk) float is widened to double for var_args)
Line 46: Line 46:
 
@6@ Converts an unsigned integer to a string with the same content as what
 
@6@ Converts an unsigned integer to a string with the same content as what
 
@@{{c|std::sprintf(buf, "%llu", value)}} would produce for sufficiently large {{tt|buf}}.
 
@@{{c|std::sprintf(buf, "%llu", value)}} would produce for sufficiently large {{tt|buf}}.
@7@ Converts a floating point value to a string with the same content as what
+
@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}}.
 
@@{{c|std::sprintf(buf, "%f", value)}} would produce for sufficiently large {{tt|buf}}.
@8@ 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}}.
 
 
@9@ Converts a floating point value to a string with the same content as what  
 
@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}}.
 
@@{{c|std::sprintf(buf, "%Lf", value)}} would produce for sufficiently large {{tt|buf}}.

Revision as of 03:03, 3 August 2021

 
 
 
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.

1) Converts a signed 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 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 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 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 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 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

Exceptions

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

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 locale for formatting purposes, and therefore concurrent calls to std::to_string from multiple threads may result in partial serialization of calls. C++17 provides std::to_chars as a higher-performance locale-independent alternative.

Example

#include <iostream>
#include <string>
 
int main() 
{
    double f = 23.43;
    double f2 = 1e-9;
    double f3 = 1e40;
    double f4 = 1e-40;
    double f5 = 123456789;
    std::string f_str = std::to_string(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".
    std::string f_str4 = std::to_string(f4); // Note: returns "0.000000"
    std::string f_str5 = std::to_string(f5);
    std::cout << "std::cout: " << f << '\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:

std::cout: 23.43
to_string: 23.430000
 
std::cout: 1e-09
to_string: 0.000000
 
std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
 
std::cout: 1e-40
to_string: 0.000000
 
std::cout: 1.23457e+08
to_string: 123456789.000000

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]