Last modified: Feb 02, 2026 By Alexander Williams
Python Floor Function Guide: math.floor() Explained
Working with numbers is a core part of programming. You often need to round them. Python provides a powerful tool for this: the floor function.
This guide explains everything about math.floor(). You will learn what it does, how to use it, and where to apply it.
What is the Floor Function?
The floor function rounds a number down. It goes to the nearest integer less than or equal to the original number.
Think of it like moving down a floor in a building. You always go to the floor below or stay on the same one if you are already there.
For example, the floor of 3.7 is 3. The floor of 5.0 is 5. The floor of -2.3 is -3.
It always moves towards negative infinity. This is key for handling negative numbers.
How to Use math.floor() in Python
Python's floor function lives in the math module. You must import this module first.
The basic syntax is simple. You call math.floor(x) where x is your number.
Understanding this syntax is a fundamental part of your Python Function Syntax Guide for Beginners.
# First, import the math module
import math
# Round some numbers down
result1 = math.floor(4.95)
result2 = math.floor(7.1)
result3 = math.floor(-3.4)
print(result1)
print(result2)
print(result3)
4
7
-4
Notice the last result. math.floor(-3.4) returns -4, not -3. This is the "down towards negative infinity" rule in action.
math.floor() vs. int() vs. // Operator
Python has other ways to get integer values. It is important to know the difference.
The int() function truncates. It simply removes the decimal part.
The floor division operator // performs division and then applies the floor function to the result.
import math
value = -5.7
print("Using math.floor():", math.floor(value)) # Rounds DOWN
print("Using int():", int(value)) # Truncates (cuts off decimal)
print("Using // with 1:", value // 1) # Floor division by 1
Using math.floor(): -6
Using int(): -5
Using // with 1: -6.0
The key difference is with negative numbers. For positive numbers, int() and math.floor() often give the same result.
Practical Examples of math.floor()
Let's see where you might use the floor function in real code.
Example 1: Calculating Whole Items from a Total
Imagine you are building a shopping cart. You have a total quantity and items per pack.
You need to know how many full packs you can make.
import math
total_items = 47
items_per_pack = 6
full_packs = math.floor(total_items / items_per_pack)
leftover_items = total_items % items_per_pack # Modulo for remainder
print(f"Full packs: {full_packs}")
print(f"Leftover items: {leftover_items}")
Full packs: 7
Leftover items: 5
Example 2: Converting Units
You can use it to convert seconds into whole minutes, ignoring leftover seconds.
import math
total_seconds = 325
minutes = math.floor(total_seconds / 60)
seconds_remainder = total_seconds % 60
print(f"{total_seconds} seconds is {minutes} minutes and {seconds_remainder} seconds.")
325 seconds is 5 minutes and 25 seconds.
Example 3: Pagination Logic
A common web task is pagination. If you have 120 items and show 10 per page, how many pages are needed?
import math
total_items = 120
items_per_page = 10
# Calculate total pages, always rounding up for leftover items
total_pages = math.ceil(total_items / items_per_page) # ceil rounds up
# Or, calculate full pages using floor
full_pages = math.floor(total_items / items_per_page)
print(f"Total pages needed (using ceil): {total_pages}")
print(f"Completely full pages (using floor): {full_pages}")
Total pages needed (using ceil): 12
Completely full pages (using floor): 11
Common Errors and How to Fix Them
Here are some mistakes beginners make and how to solve them.
Error 1: Forgetting to Import math
You will get a NameError if you try to use math.floor() without importing.
# This will cause an error
result = math.floor(5.5) # NameError: name 'math' is not defined
Fix: Always start your script with import math.
Error 2: Passing the Wrong Argument Type
math.floor() expects a number. Passing a string causes a TypeError.
import math
# This will cause an error
result = math.floor("10.5") # TypeError: must be real number, not str
Fix: Convert the string to a float first using float().
import math
result = math.floor(float("10.5"))
print(result) # Output: 10
Advanced Usage: Working with Data Structures
You can apply math.floor() to lists or other sequences using a loop or a list comprehension.
This is where understanding Python Function Argument Unpacking can become useful for more complex data handling.
import math
prices = [19.99, 5.50, 12.75, 23.20]
# Apply floor to every price in the list using a list comprehension
floored_prices = [math.floor(price) for price in prices]
print("Original prices:", prices)
print("Floored prices:", floored_prices)