Python int() 到最接近的值

Python int() to the nearest value

python int()函数的工作方式是将数字四舍五入到最小值。

这样 4.99 = 4

我想让4.99等于5怎么办?

int() 截断 浮点值,删除数字的非整数部分。您正在寻找 舍入

您可以为此使用 round() function

>>> round(4.99)
5

有一个 round() 内置函数:

>>> round(4.99)
5

math.ceil and math.floor return 浮点数是参数上方或下方最接近的整数。