python 向上 向下 就近取整

向上取整

ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数。

ceil()是不能直接访问的,需要导入 math 模块。

>>> import math
>>> print(math.ceil(5.1))
6
>>> print(math.ceil(5.5))
6
>>> print(math.ceil(5.6))
6
>>> 

向下取整

floor(x) 返回数字的下舍整数,小于或等于 x

>>> print(math.floor(5.1))
5
>>> print(math.floor(5.5))
5
>>> print(math.floor(5.6) )
5
>>> 

就近取整

round(X)是内战函数,将浮点数圆整为与之最接近的整数,并且在与两边整数一样近的时候圆整到偶数

>>> print(round(4.1))
4
>>> print(round(4.5))
4
>>> print(round(5.5))
6
>>> print(round(4.6))
5
>>> 

猜你喜欢

转载自blog.csdn.net/qq_20831401/article/details/121523724