Function int() rounding towards negative infinity (floor) or zero?

谁说胖子不能爱 提交于 2019-12-01 18:20:37

int() removes the decimal component; it doesn't do any rounding. From the documentation:

If x is floating point, the conversion truncates towards zero.

For turning a float into an int this is entirely logical behaviour. This is not division, flooring or otherwise.

The // floor division operator otherwise clearly does floor, not truncate. In Python 2, for two integer operands, the / division also floors. The documentation again:

the result is that of mathematical division with the ‘floor’ function applied to the result

where math.floor() is documented as:

Return the floor of x as a float, the largest integer value less than or equal to x.

I see no inconsistency here; division floors, turning floats to integers truncates.

Maybe round() with ndigits = 0 is more close to what you're expecting. But round() doesn't return an integer, but a float: see the documentation

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