根据我使用的编译器,我在 n=0 时得到该函数的不同输出。
std::string ToStrWPrec(double a_value, const int n)
{
std::ostringstream out;
out << std::setprecision(n) << a_value;
return out.str();
}
Run Code Online (Sandbox Code Playgroud)
(GCC) 4.8.3 20140911 (Red Hat 4.8.3-9) 对于 ToStrWPrec(1.2345678,0) 返回 1。对于相同的代码,VS2013 返回 1.2346。
我的问题是:
这是根据下面的评论更新的代码
std::string ToStrWPrec(double a_value, const int n)
{
std::ostringstream out;
out << std::setprecision(n) << std::fixed<< a_value;
return out.str();
}
Run Code Online (Sandbox Code Playgroud)