Why round(4.5) == 4 and round(5.5) == 6 in Python 3.5? [duplicate]

你。 提交于 2019-12-02 09:03:35

In Python 3, exact half way numbers are rounded to the nearest even result. This behavior changed in Python 3

The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) round(x[, n]) now delegates to x.round([n]) instead of always returning a float. It generally returns an integer when called with a single argument and a value of the same type as x when called with two arguments.

Python 3 uses Bankers Rounding, which rounds .5 values to the closest even number.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!