round() 似乎没有正确舍入
round() 函数的文档指出您向它传递了一个数字,以及小数点后要四舍五入的位置。 因此它应该这样做:
n = 5.59
round(n, 1) # 5.6
但是,实际上,古老的浮点怪异现象悄然出现,你会得到:
5.5999999999999996
出于 UI 的目的,我需要显示 5.6
。 我在互联网上浏览了一下,发现了一些 文档取决于我的Python实现。 不幸的是,这种情况发生在我的 Windows 开发机器和我尝试过的每台 Linux 服务器上。 另请参阅此处。
除了创建自己的圆形库之外,还有什么办法可以解决这个问题吗?
The documentation for the round() function states that you pass it a number, and the positions past the decimal to round. Thus it should do this:
n = 5.59
round(n, 1) # 5.6
But, in actuality, good old floating point weirdness creeps in and you get:
5.5999999999999996
For the purposes of UI, I need to display 5.6
. I poked around the Internet and found some documentation that this is dependent on my implementation of Python. Unfortunately, this occurs on both my Windows dev machine and each Linux server I've tried. See here also.
Short of creating my own round library, is there any way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
仅当最后一位数字为 5 时才会出现问题。 0.045 在内部存储为 0.044999999999999...您可以简单地将最后一位数字增加到 6 并四舍五入。 这会给你想要的结果。
The problem is only when last digit is 5. Eg. 0.045 is internally stored as 0.044999999999999... You could simply increment last digit to 6 and round off. This will give you the desired results.
作品完美
Works Perfect
代码:
输出:
Code:
Output:
关于什么:
What about:
我在使用 round() 时遇到了同样的错误。 现在,我像这样使用 ceil() 或 Floor() :
I had the same error when using round(). Now, I use ceil() or floor() like so:
这是一种将浮点数四舍五入到任意小数位的简单方法,并且在 2021 年仍然有效!
这将打印出来;
Here is an easy way to round a float number to any number of decimal places, and it still works in 2021!
And this will print;
这就是我看到回合失败的地方。 如果您想将这 2 个数字四舍五入到小数点后一位怎么办?
23.45
23.55
我的教育是,通过四舍五入你应该得到:
23.4
23.6
“规则”是,如果前面的数字是奇数,则应向上舍入,如果前面的数字是偶数,则不向上舍入。
python 中的 round 函数只是截断 5。
Here's where I see round failing. What if you wanted to round these 2 numbers to one decimal place?
23.45
23.55
My education was that from rounding these you should get:
23.4
23.6
the "rule" being that you should round up if the preceding number was odd, not round up if the preceding number were even.
The round function in python simply truncates the 5.
我正在做:
在这种情况下,我们首先在单位级别正确舍入,然后转换为整数以避免打印浮点数。
所以
我认为这个答案比格式化字符串更好,而且对我来说使用 round 函数也更有意义。
I am doing:
In this case, we first round properly at the unit level, then we convert to integer to avoid printing a float.
so
I think this answer works better than formating the string, and it also makes more sens to me to use the round function.
您可以使用字符串格式运算符
%
,类似于 sprintf。You can use the string format operator
%
, similar to sprintf.另一个潜在的选择是:
Another potential option is:
在这种情况下,我会完全避免依赖
round()
。 如果您需要可靠舍入到最接近的整数,请考虑将输出
不是所需的输出。 要绕过此行为,请使用
math.ceil()
(或math.floor()
如果您想向下舍入):输出
希望有所帮助。
I would avoid relying on
round()
at all in this case. Considerwill output
which is not a desired output if you need solid rounding to the nearest integer. To bypass this behavior go with
math.ceil()
(ormath.floor()
if you want to round down):outputs
Hope that helps.
printf 傻瓜。
printf the sucker.
这确实是一个大问题。 试试这个代码:
它显示 4.85。 然后你就可以了:
它显示 4.8。 您是否手动计算,确切的答案是 4.85,但如果您尝试:
您可以看到真相:浮点存储为分母为 2 的幂的分数的最接近的有限和。
It's a big problem indeed. Try out this code:
It displays 4.85. Then you do:
and it shows 4.8. Do you calculations by hand the exact answer is 4.85, but if you try:
you can see the truth: the float point is stored as the nearest finite sum of fractions whose denominators are powers of two.
浮点数学很容易出现轻微但烦人的精度误差。 如果您可以使用整数或定点,则将保证精度。
Floating point math is vulnerable to slight, but annoying, precision inaccuracies. If you can work with integer or fixed point, you will be guaranteed precision.
您可以将数据类型切换为整数:
然后通过插入区域设置的小数分隔符来显示数字。
然而,吉米的答案更好。
You can switch the data type to an integer:
And then display the number by inserting the locale's decimal separator.
However, Jimmy's answer is better.
看一下 Decimal 模块
和
Decimal 提供了一种运算,可以轻松编写需要浮点运算的应用程序,并且还需要以人类可读的格式(例如会计)呈现这些结果。
Take a look at the Decimal module
and
Decimal provides the kind of operations that make it easy to write apps that require floating point operations and also need to present those results in a human readable format, e.g., accounting.
如果执行
str(round(n, 1))
而不仅仅是round(n, 1)
,则会得到“5.6”。You get '5.6' if you do
str(round(n, 1))
instead of justround(n, 1)
.round(5.59, 1)
工作正常。 问题是5.6无法用二进制浮点数精确表示。正如 Vinko 所说,您可以使用字符串格式来进行显示舍入。
如果您需要的话,Python 有一个十进制算术模块。
round(5.59, 1)
is working fine. The problem is that 5.6 cannot be represented exactly in binary floating point.As Vinko says, you can use string formatting to do rounding for display.
Python has a module for decimal arithmetic if you need that.
如果您使用 Decimal 模块,您可以在不使用“round”函数的情况下进行近似。 这是我一直使用的舍入方法,尤其是在编写货币应用程序时:
这将返回一个十进制数,即 16.20。
If you use the Decimal module you can approximate without the use of the 'round' function. Here is what I've been using for rounding especially when writing monetary applications:
This will return a Decimal Number which is 16.20.
即使无需舍入,格式也能正常工作:
Formatting works correctly even without having to round:
我无法改变它的存储方式,但至少格式可以正常工作:
I can't help the way it's stored, but at least formatting works correctly: