math.ceil(x) returns the ceiling of x: the smallest integer greater than or equal to x, returned as an int.
On negative numbers, rounding upward moves toward zero: math.ceil(-2.5) is -2, whereas math.floor(-2.5) goes down to -3.
math.ceil() is a function in Python's standard math module: import it with import math and call math.ceil(...), or import the name directly with from math import ceil.
Syntax
Import math and pass any finite real number.
python
import math
n = math.ceil(x)
Call
What it does
math.ceil(2.1)
Returns 3.
math.ceil(-2.5)
Returns -2 (upward toward zero).
math.ceil(4.0)
Returns 4 (an int).
math.ceil(float("inf"))
Raises OverflowError: cannot convert infinity.
In Python 3, math.ceil() returns an int (unlike Python 2 which returned a float). For exact integer division rounding up without floats, use (total + size - 1) // size.
Function signatures
python
math.ceil(x, /)
# returns: int smallest integer >= x (calls x.__ceil__())
Parameters
Parameter
Description
x
Required positional finite real number or object defining __ceil__().
Return Value
Return Value
Description
int
Smallest integer not less than x.
Python Examples
Example 1 — Count Shipping Boxes Needed With math.ceil()
Round partial box counts upward and compare with integer division ceiling (items + cap - 1) // cap.
python
import math
cap = 10
for items in [1, 10, 11, 25]:
boxes = math.ceil(items / cap)
exact_int = (items + cap - 1) // cap
print(f"items={items:>2} -> ceil={boxes} exact={exact_int}")
Upward on negatives. Every negative value between -3 and -2 rounds upward to -2.
Floor goes down.math.floor() drops those same negatives down to -3.
Trunc matches ceil for negatives. Both math.trunc() and ceil() yield -2 on negative fractions, but diverge on positive 2.1.
Example 3 — Round Up Fees to Cents and Handle Inf / NaN
Scale decimal prices by 100 to round up to cents, and catch non-finite conversion errors.
python
import math
for fee in [4.121, 9.005, 1.999]:
cents_up = math.ceil(fee * 100) / 100.0
print(f"{fee:.3f} -> {cents_up:.2f}")
for bad_v in [float("inf"), float("nan")]:
try:
math.ceil(bad_v)
except (OverflowError, ValueError):
print(f"cannot take ceil of {bad_v}")
Output
4.121 -> 4.13
9.005 -> 9.01
1.999 -> 2.00
cannot take ceil of inf
cannot take ceil of nan
How It Works
Scale before ceiling. Multiplying by 100 turns cents into whole integers so any sub-cent fraction bumps upward.