我刚刚重读了What’s New In Python 3.0页,上面写着:
round()函数的舍入策略和返回类型已更改.
和 round:
对于支持round()的内置类型,值被舍入到
所以,v2.7.3岁以下:
In [85]: round(2.5)
Out[85]: 3.0
In [86]: round(3.5)
Out[86]: 4.0
正如我所料.然而,现在不到v3.2.3岁:
In [32]: round(2.5)
Out[32]: 2
In [33]: round(3.5)
Out[33]: 4
这似乎违反直觉,与我的理解相反
- 有人知道为什么会变成这样吗?
- 还有没有其他主流编程语言(例如,C, C++, Java, Perl,.)做这种(对我来说不一致的)取整的?
我错过了什么吗?
UPDATE: @Li-aungYip's comment re "Banker's rounding" gave me the right search term/keywords to search for and I found this SO question: Why does .NET use banker's rounding as default?, so I will be reading that carefully.