题
我有一些问题想双重转换为C ++字符串。这是我的代码
std::string doubleToString(double val)
{
std::ostringstream out;
out << val;
return out.str();
}
我的问题是,如果一个双被传递在为“千万”。然后被返回的字符串值是1E + 007
我怎样才能获得字符串值作为“10000000”
解决方案
#include <iomanip>
using namespace std;
// ...
out << fixed << val;
// ...
您还可以考虑使用setprecision
设置的小数位数:
out << fixed << setprecision(2) << val;
其他提示
#include <iomanip>
std::string doubleToString(double val)
{
std::ostringstream out;
out << std::fixed << val;
return out.str();
}
您还可以设置最小宽度和填充字符用STL IO操纵器,如:
out.width( 9 ); out.fill( ' ' );
不隶属于 StackOverflow