std::to_string
来自cppreference.com
< cpp | string | basic string
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
定义于头文件 <string>
|
||
std::string to_string( int value ); |
(1) | (C++11 起) |
std::string to_string( long value ); |
(2) | (C++11 起) |
std::string to_string( long long value ); |
(3) | (C++11 起) |
std::string to_string( unsigned value ); |
(4) | (C++11 起) |
std::string to_string( unsigned long value ); |
(5) | (C++11 起) |
std::string to_string( unsigned long long value ); |
(6) | (C++11 起) |
std::string to_string( float value ); |
(7) | (C++11 起) |
std::string to_string( double value ); |
(8) | (C++11 起) |
std::string to_string( long double value ); |
(9) | (C++11 起) |
5)
std::sprintf(buf, "%lu", value)会产生足够大的
buf
了同样内容的一个字符串转换成一个无符号十进制整数.原文:
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)
std::sprintf(buf, "%llu", value)会产生足够大的
buf
了同样内容的一个字符串转换成一个无符号十进制整数.原文:
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 @std::sprintf(buf, "%f", value)会产生足够大的
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)
std::sprintf(buf, "%Lf", value)会产生足够大的
buf
了同样内容的一个字符串转换成一个浮点值.原文:
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
.目录 |
[编辑] 参数
value | - | 需要转换的数值 |
[编辑] 返回值
一个包含转换后值的字符串
[编辑] 示例
运行此代码
#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'; }
输出:
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
[编辑] 另请参阅
(C++11) |
将整值或浮点值转换成wstring (函数) |
(C++11) (C++11) |
(函数) |
(C++11) (C++11) (C++11) |
(函数) |
(C++11) (C++11) (C++11) |
(函数) |
(C++17) |
转换整数或浮点值到字符序列 (函数) |