all Python 3mathterm
math.ceil
rounding
Rounds up to the nearest integer. math.ceil(2.1) → 3, math.ceil(-2.1) → -2.
Syntax
math.ceil(x)
It returns an int, not a float, and defers to the object's __ceil__ method — Decimal and Fraction implement it, so they round exactly with no loss of digits. For ceiling division of integers, though, write -(-a // b): math.ceil(a / b) builds a float quotient first, and on large values that rounding gives the wrong answer.
Examples
import math
print(math.ceil(3.1)) # → 4
print(math.ceil(3.9)) # → 4
print(math.ceil(-3.1)) # → -3
print(math.ceil(-3.9)) # → -3
print(math.ceil(5.0)) # → 5
print(math.ceil(0.001)) # → 1
See also
Open in the full glossarydocs.python.orgReport an inaccuracy