Python math 模块

Python math.ceil() 函数用于向上取整,返回不小于 x 的最小整数。

语法

math.ceil(x)

参数

x:数字。

返回值

返回不小于 x 的最小整数(int),向正无穷取整。

示例

import math

print(math.ceil(3.2))     # 4
print(math.ceil(3.7))     # 4
print(math.ceil(-3.2))    # -3(向上!)
print(math.ceil(5.0))     # 5

# 典型场景:分页计算总页数
total = 23
per_page = 10
pages = math.ceil(total / per_page)
print(pages)              # 3

输出:

4
4
-3
5
3

注意事项

ceil 对负数向上(-3.2 → -3)。算分页数、需要进一时用它。