Python常用取整函数
1.向0取整: int ()
print(int(6.5))
6
print(int(-2.6))
-2
2.向上取整: math.ceil ()
import math
print(math.ceil(6.4))
7
print(math.ceil(-6.4))
-6
注意:“向上取整”是往更大的取
3.向下取整: math.floor ()
import math
print(math.floor(2.6))
2
print(math.floor(-2.6))
-3
注意:“向下取整”是往更小的取
3.四舍五入: round ()
print(round(7.2))
7
print(round(7.5))
8
补充:
将 整数部分 与 小数部分 分开:math.modf ()
import math
print(math.modf(3.56))
(0.56, 3.0)
返回的是元组