我正在使用Python 3.5.3,我有一个奇怪的舍入行为
round(1.5) Out[16]: 2 round(2.5) Out[17]: 2 round(3.5) Out[18]: 4
为什么不是四舍五入(2.5)四舍五入到3?
The Python Documentation 说:
如果两个倍数相等接近,则向偶数选择取整(例如,两者都是如此) round(0.5) round(-0.5) 为0,并且 round(1.5) 是 2 ).
round(0.5)
round(-0.5)
round(1.5)
2
import math def round(number): if (math.ceil(number) - number >= number - math.floor(number)): return math.ceil(number) else: return math.floor(number)
测试时间: https://repl.it/KHJY