我有一个需要放入std::string的浮点值。如何从float转换为string?
float val = 2.5;
std::string my_val = val; // error here发布于 2010-01-24 12:01:55
除非您担心性能问题,否则请使用string streams
#include <sstream>
//..
std::ostringstream ss;
ss << myFloat;
std::string s(ss.str());如果你对Boost没意见,lexical_cast<>是一个很方便的选择:
std::string s = boost::lexical_cast<std::string>(myFloat);有效的替代方法是例如FastFormat或简单的C风格的函数。
发布于 2013-06-19 03:22:35
从C++11开始,标准C++库为函数std::to_string(arg)提供了arg支持的各种类型。
发布于 2016-02-12 00:56:53
重要
阅读结尾处的注释。
快速解答:
使用to_string()。(从c++11开始提供)
示例:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string pi = "pi is " + to_string(3.1415926);
cout<< "pi = "<< pi << endl;
return 0;
}自己运行:http://ideone.com/7ejfaU
这些也是可用的:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);重要说明:
正如@Michael Konečn As正确地指出的那样,使用to_string()充其量是有风险的,很可能会导致意想不到的结果。
来自http://en.cppreference.com/w/cpp/string/basic_string/to_string:
使用浮点类型std::to_string的
可能会产生意外的结果,因为返回字符串中的有效位数可以为零,请参见示例。
返回值可能与std::cout默认打印的值有很大不同,请参见示例。出于格式化的目的,std::to_string依赖于当前的区域设置,因此从多个线程对std::to_string的并发调用可能导致调用的部分序列化。C++17提供了std::to_chars作为一种性能更高的独立于区域设置的替代方案。
最好的方法是像在his answer中演示的@dcp那样使用stringstream:
以下示例演示了此问题:
自己运行示例:https://www.jdoodle.com/embed/v0/T4k
#include <iostream>
#include <sstream>
#include <string>
template < typename Type > std::string to_str (const Type & t)
{
std::ostringstream os;
os << t;
return os.str ();
}
int main ()
{
// more info : https://en.cppreference.com/w/cpp/string/basic_string/to_string
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'
<< "ostringstream: " << to_str (f) << "\n\n"
<< "std::cout: " << f2 << '\n'
<< "to_string: " << f_str2 << '\n'
<< "ostringstream: " << to_str (f2) << "\n\n"
<< "std::cout: " << f3 << '\n'
<< "to_string: " << f_str3 << '\n'
<< "ostringstream: " << to_str (f3) << "\n\n"
<< "std::cout: " << f4 << '\n'
<< "to_string: " << f_str4 << '\n'
<< "ostringstream: " << to_str (f4) << "\n\n"
<< "std::cout: " << f5 << '\n'
<< "to_string: " << f_str5 << '\n'
<< "ostringstream: " << to_str (f5) << '\n';
return 0;
}输出:
std::cout: 23.43
to_string: 23.430000
ostringstream: 23.43
std::cout: 1e-09
to_string: 0.000000
ostringstream: 1e-09
std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
ostringstream: 1e+40
std::cout: 1e-40
to_string: 0.000000
ostringstream: 1e-40
std::cout: 1.23457e+08
to_string: 123456789.000000
ostringstream: 1.23457e+08 https://stackoverflow.com/questions/2125880
复制相似问题