C++ 限制浮点精度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3383817/
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-28 12:43:51  来源:igfitidea点击:

Limit floating point precision?

c++calgorithmfloating-pointfloating-point-conversion

提问by jmasterx

Is there a way to round floating points to 2 points? E.g.: 3576.7675745342556becomes 3576.76.

有没有办法将浮点四舍五入到 2 点?例如:3576.7675745342556变成3576.76

回答by strager

round(x * 100) / 100.0

If you must keep things floats:

如果你必须保持浮动:

roundf(x * 100) / 100.0

Flexible version using standard library functions:

使用标准库函数的灵活版本:

double GetFloatPrecision(double value, double precision)
{
    return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision)); 
}

回答by carlsborg

If you are printing it out, instead use whatever print formatting function available to you.

如果您要打印出来,请改用您可用的任何打印格式功能。

In c++

在 C++ 中

cout << setprecision(2) << f; 

For rounding to render to GUI, use std::ostringstream

要舍入以呈现到 GUI,请使用 std::ostringstream

回答by xinthose

For those of you googling to format a float to money like I was:

对于那些像我一样使用谷歌搜索将浮点数格式化为货币的人:

#include <iomanip>
#include <sstream>
#include <string>

std::string money_format (float val)
{
    std::ostringstream oss;

    oss << std::fixed << std::setfill ('0') << std::setprecision (2) << val;

    return oss.str();
}
// 12.3456 --> "12.35"
// 1.2 --> "1.20"

You must return it as a string. Putting it back into a float will lose the precision.

您必须将其作为字符串返回。将它放回浮点数会失去精度。

回答by Tassos Bassoukos

Multiply by 100, round to integer (anyway you want), divide by 100. Note that since 1/100 cannot be represented precisely in floating point, consider keeping fixed-precision integers.

乘以 100,四舍五入为整数(无论如何你想要),除以 100。请注意,由于 1/100 不能用浮点精确表示,请考虑保留固定精度整数。

回答by R.. GitHub STOP HELPING ICE

Don't use floats. Use integers storing the number of cents and print a decimal point before the last 2 places if you want to print dollars. Floats are almost always wrongfor money unless you're doing simplistic calculations (like naive economic mathematical models) where only the magnitude of the numbers really matters and you never subtract nearby numbers.

不要使用浮动。如果要打印美元,请使用存储美分数的整数并在最后 2 位之前打印小数点。浮点数对于金钱来说几乎总是错误的,除非你在做简单的计算(比如简单的经济数学模型),其中只有数字的大小才是真正重要的,而且你永远不会减去附近的数字。

回答by Yassine Abdul-Rahman

try use

尝试使用

std::cout<<std::setprecision(2)<<std::cout<<x;

std::cout<<std::setprecision(2)<<std::cout<<x;

should works and only 2 digit after the floating point appear.

应该可以工作,并且浮点数出现后只有 2 位数字。

回答by Adam Marsono Putra

Try this, it works perfectly

试试这个,它完美地工作

float=3576.7675745342556;
printf("%.2f",float);

change some objects in it to see and learn the code.

更改其中的一些对象以查看和学习代码。

回答by Doug

To limit the precision:
If x is a float, no rounding:
(shift up by 2 decimal digits, strip the fraction, shift down by 2 decimal digits)

限制精度:
如果 x 是浮点数,则不四舍五入:(上
移 2 位小数,去掉分数,下移 2 位小数)

((int)(x*100.0)) / 100.0F

Float w/ rounding:

带四舍五入的浮动:

((int)(x*100.0 + 0.5F)) / 100.0F

Double w/o rounding:

双无四舍五入:

((long int)(x*100.0)) / 100.0

Double w/ rounding:

双带四舍五入:

((long int)(x*100.0 + 0.5)) / 100.0

Note: Because x is either a floator a double, the fractional part will always be there. It is the difference between how a # is represented (IEEE 754) and the #'s precision.
C99 supports round()

注意:因为 x 要么是 a 要么是floata double,所以小数部分将始终存在。这是 # 的表示方式 ( IEEE 754) 和 # 的精度之间的区别。
C99 支持round()

回答by Controler

yourFloatNumber= Float.Round(yourFloatNumber,2); // In C#