Python math.ceil() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

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)
CallWhat 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

ParameterDescription
xRequired positional finite real number or object defining __ceil__().

Return Value

Return ValueDescription
intSmallest 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}")

How It Works

  1. Any remainder needs another box. Eleven items require two capacity-10 boxes, so math.ceil(1.1) returns 2.
  2. Exact multiples stay put. Ten items fit cleanly in one box without bumping to two.
  3. Pure-int alternative. Adding cap - 1 before floor division // matches math.ceil() while staying safe for huge integers.

Example 2 — Compare ceil(), floor(), and trunc() on Negative Numbers

See how moving toward positive infinity differs from flooring and truncation.

python
import math

for val in [-2.9, -2.5, -2.1, 2.1]:
    c, fl, tr = math.ceil(val), math.floor(val), math.trunc(val)
    print(f"{val:>4} -> ceil={c:>2} floor={fl:>2} trunc={tr:>2}")

How It Works

  1. Upward on negatives. Every negative value between -3 and -2 rounds upward to -2.
  2. Floor goes down. math.floor() drops those same negatives down to -3.
  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}")

How It Works

  1. Scale before ceiling. Multiplying by 100 turns cents into whole integers so any sub-cent fraction bumps upward.
  2. Infinity overflows integer conversion. Calling math.ceil(inf) raises OverflowError.
  3. NaN has no integer order. Passing nan raises ValueError.

Frequently Asked Questions

An int representing the smallest whole integer greater than or equal to x.
-2 is greater than -2.5, so rounding up (toward positive infinity) moves from -2.5 to -2. Use math.floor(-2.5) for -3.
In Python 3 it returns the integer 4.
math.inf raises OverflowError and math.nan raises ValueError.
Use (a + b - 1) // b for positive integers a and b, which stays completely in exact integer arithmetic.

Next: math.comb()

Return the number of ways to choose k items from n without order.

comb() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful