C++ 将双精度舍入到小数点后 3 点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14369673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:16:45  来源:igfitidea点击:

Round double to 3 points decimal

c++rounding

提问by Tom

Currently, I can round a doubleto an output stream using:

目前,我可以double使用以下方法将a 舍入到输出流:

output.setf(std::ios::fixed,std::ios::floatfield);
output.precision(3);

But I'm given a doubleand I need to make the conversion before I insert it to a vector. So for instance, if the number -0.00078appears then it equals to 0.000and I won't need to save it. On the other hand, 1.0009will become 1.001(same as the precision function handles it).

但是我得到了一个double,我需要在将它插入向量之前进行转换。因此,例如,如果-0.00078出现该数字,则它等于0.000并且我不需要保存它。另一方面,1.0009将变为1.001(与精度函数处理它相同)。

How can I convert doubles like that in C++?

如何在 C++ 中像这样转换双打?

回答by paddy

A common trick is to do it with maths:

一个常见的技巧是用数学来做:

value = round( value * 1000.0 ) / 1000.0;

Where roundwill handle negative and positive values correctly... Something like this (untested):

哪里round将正确处理负值和正值......像这样的东西(未经测试):

inline double round( double val )
{
    if( val < 0 ) return ceil(val - 0.5);
    return floor(val + 0.5);
}

You'll still want to set the decimal places to 3 during output, due to floating point precision problems.

由于浮点精度问题,您仍然希望在输出期间将小数位设置为 3。

回答by Oliver Charlesworth

Other answers here have given you a technique. But it's important to mention that not all values can be exactly represented in floating-point. 1.001 is a good example; the nearest possible value is 1.00099999999999988987.

此处的其他答案为您提供了一种技巧。但值得一提的是,并非所有值都可以用浮点数精确表示。1.001 是一个很好的例子;最接近的可能值是 1.00099999999999988987。

So if your aim is to get strictly 3 decimal places, then the answer is: that's not possible.

因此,如果您的目标是严格保留 3 位小数,那么答案是:这是不可能的。

回答by classyGrrrl

I know this is a very old post but I was looking for a solution to the same problem. However, I did not want to create a special function for it so I came up with the following:

我知道这是一篇很老的帖子,但我一直在寻找解决同一问题的方法。但是,我不想为它创建一个特殊的函数,所以我想出了以下内容:

#include <sstream>
#include <iomanip>
...
...
...
double val = 3.14159;
stringstream tmp;
tmp << setprecision(3) << fixed << val;
double new_val = stod(tmp.str());   // new_val = 3.143
tmp.str(string());                  // clear tmp for future use

Not sure if this is the best way to do it but it worked for me!

不确定这是否是最好的方法,但它对我有用!

回答by Scott Hunter

You can multiply it by 1000 and then round (or truncate) it; this will give you a value 1000 times the 3-decimal place value. Note that, if you divide it by 1000 to get the 'rounded' value, you may end up w/ more than 3 decimal places (due to round off error).

您可以将其乘以 1000,然后舍入(或截断)它;这将为您提供 3 位小数位值的 1000 倍。请注意,如果将其除以 1000 以获得“四舍五入”值,则最终可能会保留超过 3 个小数位(由于四舍五入错误)。