1. round函数
四舍五入到最接近的整数。
例如:
import math
x =1.55
y = -1.55
print(round(x))
print(round(y))
输出:
2
-2
第二个参数给出要四舍五入的小数位数(默认为 0)
print(round(x, 1))
print(round(y, 1))
这次输出:
1.6
-1.6
2. floor 函数
得到小于参数的最大整数
print(math.floor(x))
print(math.floor(y))
输出:
1
-2
3. ceil 函数
获得大于 参数的最小整数
print(math.ceil(x))
print(math.ceil(y))
输出:
2
-1
4. trunc函数
删除参数的小数部分
print(math.trunc(x))
print(math.trunc(y))
输出:
1
-1
网友评论