我正在使用 Python 2.0。我尝试执行 -9/4 并期望答案是 -2。但是,它返回 -3。现在 -9/4 是 -2.25 我认为 int 类型会将它四舍五入为 -2 而不是 -3。当我执行 int(-2.25) 时,它返回 -2。谁能解释一下为什么 int(-9/4) 返回 -3 而 int(-2.25) 返回 -2?
请您参考如下方法:
这两个操作只是具有不同的舍入模式。来自 int() 文档,( reference )
If x is floating point, the conversion truncates towards zero.
除法四舍五入,( reference )
Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the ‘floor’ function applied to the result.
