小编Pay*_*man的帖子

GCC 与 VS2013 中 std::set precision(0) 的正确行为

根据我使用的编译器,我在 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。

我的问题是:

  1. set precision 的正确/标准行为是什么?
  2. 使用 set precision 的更好替代方案是什么?

这是根据下面的评论更新的代码

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)

c++ gcc visual-studio-2013

5
推荐指数
1
解决办法
1057
查看次数

标签 统计

c++ ×1

gcc ×1

visual-studio-2013 ×1