math.ceil() and math.floor() Explained

math.ceil() and math.floor() are useful when you need to round numbers in a specific direction.

  • math.ceil() rounds up
  • math.floor() rounds down

These functions are part of Python’s math module overview, so you must import math before using them.

If you are comparing them with round(), the key difference is simple:

  • math.ceil() always goes up
  • math.floor() always goes down
  • round() goes to the nearest value

Quick example

import math

print(math.ceil(3.2))   # 4
print(math.floor(3.8))  # 3
print(math.ceil(-3.8))  # -3
print(math.floor(-3.2)) # -4

Use math.ceil() to round up to the next whole number. Use math.floor() to round down to the previous whole number.

What this page covers

  • math.ceil() returns the smallest integer greater than or equal to a number
  • math.floor() returns the largest integer less than or equal to a number
  • Both functions are in the math module, so you must import math first
  • These functions return integers

How math.ceil() works

math.ceil() rounds a number upward.

That means it returns the smallest integer that is still greater than or equal to the original number.

Examples

import math

print(math.ceil(2.1))   # 3
print(math.ceil(2.9))   # 3
print(math.ceil(2.0))   # 2

What to notice:

  • 2.1 becomes 3
  • 2.9 also becomes 3
  • 2.0 stays 2 because it is already a whole number

For positive numbers, “up” means moving to a larger number.

For negative numbers, “up” still means moving to a larger number, which is why it goes closer to zero.

import math

print(math.ceil(-2.7))  # -2
print(math.ceil(-2.1))  # -2

How math.floor() works

math.floor() rounds a number downward.

That means it returns the largest integer that is still less than or equal to the original number.

Examples

import math

print(math.floor(2.1))   # 2
print(math.floor(2.9))   # 2
print(math.floor(2.0))   # 2

What to notice:

  • 2.1 becomes 2
  • 2.9 also becomes 2
  • 2.0 stays 2

For positive numbers, “down” means moving to a smaller number.

For negative numbers, “down” means moving to a smaller number too, so it goes farther from zero.

import math

print(math.floor(-2.7))  # -3
print(math.floor(-2.1))  # -3

Why negative numbers confuse beginners

Negative numbers are where most confusion happens.

Many beginners think:

  • ceil() just removes the decimal part
  • floor() just removes the decimal part

That is not how these functions work.

They are based on direction on the number line:

  • Up means greater
  • Down means smaller

So for -2.7:

  • math.ceil(-2.7) is -2
  • math.floor(-2.7) is -3

Why?

  • -2 is greater than -2.7
  • -3 is smaller than -2.7

Think of the number line like this:

# -3   -2.7   -2
  • Ceiling goes to the next integer above the number
  • Floor goes to the next integer below the number

math.ceil() vs math.floor() vs round()

These three are often confused, but they do different jobs.

math.ceil()

Always rounds upward.

import math

print(math.ceil(4.2))   # 5
print(math.ceil(4.8))   # 5

math.floor()

Always rounds downward.

import math

print(math.floor(4.2))  # 4
print(math.floor(4.8))  # 4

round()

Rounds to the nearest value instead.

print(round(4.2))  # 4
print(round(4.8))  # 5

So:

  • math.ceil(4.2)5
  • math.floor(4.2)4
  • round(4.2)4

round() does not mean “always round up.”

If you want a full explanation of standard rounding rules, see round() explained.

Basic examples to include

Here are simple examples that show how both functions behave with different kinds of numbers.

Positive floats

import math

print(math.ceil(3.2))   # 4
print(math.floor(3.2))  # 3

Negative floats

import math

print(math.ceil(-3.2))   # -3
print(math.floor(-3.2))  # -4

Whole numbers

import math

print(math.ceil(5))    # 5
print(math.floor(5))   # 5

If the number already has no decimal part, both functions return the same whole number.

Real use case: pages needed

Suppose each page can hold 10 items, and you have 23 items.

You need enough pages to fit everything, even the last partial page.

import math

items = 23
items_per_page = 10

pages_needed = math.ceil(items / items_per_page)
print(pages_needed)  # 3

Why use math.ceil() here?

  • 23 / 10 is 2.3
  • You cannot use 2.3 pages
  • You need 3 full pages to hold all items

Real use case: full groups only

Now imagine you only want to count complete groups of 10.

import math

students = 23
group_size = 10

full_groups = math.floor(students / group_size)
print(full_groups)  # 2

Why use math.floor() here?

  • 23 / 10 is 2.3
  • Only 2 groups are completely full

Import requirement

math.ceil() and math.floor() are not built-in functions.

You must import the math module first:

import math

print(math.ceil(3.4))
print(math.floor(3.4))

If you forget the import, Python will raise an error.

print(math.ceil(3.4))

This causes a NameError because math has not been defined yet. If that happens, see how to fix NameError: name is not defined.

When to use each function

Choose the function based on what the result means in real life.

Use math.ceil() when partial values still need a full unit

Common examples:

  • boxes needed for items
  • pages needed for results
  • buses needed for passengers
  • batches needed to finish work

Example:

import math

passengers = 41
seats_per_bus = 20

buses_needed = math.ceil(passengers / seats_per_bus)
print(buses_needed)  # 3

Even though 41 / 20 is 2.05, you still need 3 buses.

Use math.floor() when only complete units count

Common examples:

  • full rows completed
  • completed groups
  • whole dollars below a price
  • full containers filled

Example:

import math

cookies = 47
cookies_per_box = 12

full_boxes = math.floor(cookies / cookies_per_box)
print(full_boxes)  # 3

Only 3 boxes can be completely filled.

Common mistakes

Here are the most common problems beginners run into.

Forgetting to import math

Wrong:

print(math.ceil(4.1))

Correct:

import math

print(math.ceil(4.1))

Expecting ceil() to just remove decimals

math.ceil(4.1) is not 4.

It is:

import math

print(math.ceil(4.1))  # 5

If you want to remove the decimal part instead of rounding up or down, you may want int() explained.

Expecting floor() to behave the same for negative numbers

This surprises many beginners:

import math

print(math.floor(-4.1))  # -5

That happens because -5 is the next lower integer.

Confusing math.floor() with round()

These are not the same:

import math

print(math.floor(4.9))  # 4
print(round(4.9))       # 5

floor() always goes down. round() goes to the nearest value.

Using string input without converting it first

If your number comes from input(), it starts as text.

user_value = input("Enter a number: ")
print(type(user_value))

To use it with math functions, convert it first:

import math

user_value = input("Enter a number: ")
number = float(user_value)

print(math.ceil(number))

If needed, see how to convert a string to a float in Python.

FAQ

What is the difference between math.ceil() and math.floor()?

math.ceil() rounds up to the next integer. math.floor() rounds down to the previous integer.

Do math.ceil() and math.floor() return int values?

Yes. In normal use, both return integer results.

Why does math.floor(-2.3) return -3?

Because floor goes to the greatest integer less than or equal to the value. -3 is less than -2.3, while -2 is greater than it.

Do I need to import math to use ceil() and floor()?

Yes. They are part of the math module, so use import math first.

Is math.ceil() the same as round()?

No. math.ceil() always rounds upward. round() rounds to the nearest value.

See also