c++ - double 和 oStringStream 的奇怪行为

标签 c++ ostringstream expression-evaluation

<分区>

所以我正在研究一个表达式评估器作为工作相关项目的内部组件。但是当涉及到 float 学的输出时,我有一些奇怪的行为......

求值器接受一个字符串

e.evaluate("99989.3 + 2346.4");
//should be 102335.7
//result is 102336

//this function is what returns the result as a string
template <class TYPE> std::string Str( const TYPE & t ) {
    //at this point t is equal to 102335.7
std::ostringstream os;

os << t;
    // at this point os.str() == 102336
return os.str();

似乎任何高于 e+004 科学记数法的 float 都被四舍五入到最接近的整数。任何人都可以解释为什么会发生这种情况以及我如何克服这个问题。

最佳答案

您可以使用 std::setprecision 设置精度.

std::fixed 的帮助下

关于c++ - double 和 oStringStream 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25408394/

相关文章:

c++ - C++ 中 std::cout 的奇怪行为

c++ - 在opengl程序中显示图形时遇到问题

c++ - 使用 C++ 从多个网络摄像头捕获图片

c++ - WinApi:如何为屏幕阅读器添加替代文本到所有者绘制的按钮?

c++ - 需要一个宏来从 std::ostringstream 和 << arg 列表创建 std::string

c++ - 如果没有 '&&' 的语句有效?

c++ - 推断函数模板参数的确切类型

c++ - 具有递归可变参数函数的stringstream?

c++ - ostringstream 在递增后得到错误的 int 值 [c++]

c++ - 为什么不同的 C++ 编译器对此代码给出不同的结果?