本文介绍了Python中的舍入是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例:

我对Python中的四舍五入方式有点困惑。 / p>

 >>> round(0.05,1)#这是有道理的
0.1
>>>轮(0.15,1)#这没有任何意义!为什么结果不是0.2?
0.1

同样的:

 >>> round(0.25,1)#这是有道理的
0.3
>>> (0.35,1)#在我看来应该是0.4但是评估为0.3
0.3



解决方案

这实际上是通过设计。来自Pythons的:


I am a bit confused about how rounding in Python works.
Could someone please explain why Python behaves like this?

Example:

>>> round(0.05,1) # this makes sense
0.1
>>> round(0.15,1) # this doesn't make sense! Why is the result not 0.2?
0.1

And same for:

>>> round(0.25,1) # this makes sense
0.3
>>> round(0.35,1) # in my opinion, should be 0.4 but evaluates to 0.3
0.3

Edit: So in general, there is a possibility that Python rounds down instead of rounding up. So am I to understand that the only "abnormal" thing that can happen is that Python rounds down? Or may it also get rounded up "abnormally" due to how it is stored? (I haven't found a case where Python rounded up when I expected it to round down)

解决方案

This is actually by design. From Pythons' documentation:

这篇关于Python中的舍入是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:57