需要计算的数字为N.
1.取上整: math.ceil(N)
2.取下整: math.floor(N) 或 '//'(分子//分母)
3.四舍五入取整: round(N,digits), digits表示保留小数位数,不填直接取整,取0时显示为取整的浮点数
4.舍弃小数部分直接取整数部分: math.trunc(N) 或 int(N)
代码:
N=-17/3
import math
# 取上整
print(math.ceil(N))
# 四舍五入
print(round(N), round(N,0))
# 取下整
print(math.floor(N))
print(-17//3)
# 舍弃小数部分直接取整数部分
print(int(N))
print(math.trunc(N))
运行结果:
PS: Python版本:3.10 , 编辑器:VScode