我有一些问题想双重转换为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( ' ' );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top