In this tutorial, I will explain how to floor a number in Python. Someone asked me this question in a Python webinar, As a Python developer based in New York I faced some scenarios where I needed to floor a number in Python. I will share my knowledge in flooring a number in Python with examples.
Methods
Python provides several ways to floor a number, and we’ll explore them in detail with examples.
Read Convert Int to Bytes in Python
1. Use the math.floor() Function
Python’s built-in math module provides the floor() function, which is the most simple way to floor a number. The floor() function takes a numeric value as an argument and returns the largest integer less than or equal to that value. The math.floor() function rounds down a given number to the nearest integer.
Here’s an example:
import math
score = 87.6
floored_score = math.floor(score)
print(f"John's floored score is: {floored_score}") Output:
John's floored score is: 87I have executed the above example code and added the screenshot below.

In this example, we have John’s score as 87.6. By using the math.floor() function, we round it down to 87, which is the largest integer less than or equal to 87.6.
Check out Convert DateTime to UNIX Timestamp in Python
2. Use the int() Function
Another way to floor a number in Python is by using the int() function. The int() function converts a number or a string to an integer. When used with a floating-point number, it truncates the decimal part, effectively rounding down the number. As long as your numbers are positive, you can simply convert to an int to round down to the next integer.
Example:
temperature = 72.8
floored_temperature = int(temperature)
print(f"The floored temperature in New York is: {floored_temperature}°F") Output:
The floored temperature in New York is: 72°FI have executed the above example code and added the screenshot below.

Here, we have the temperature in New York as 72.8°F. By using the int() function, we truncate the decimal part and get the floored temperature as 72°F.
Read How to Split a String into an Array in Python?
Difference between math.floor() and int()
It’s important to note that while math.floor() and int() often produce the same result, they behave differently when dealing with negative numbers. floor() rounds down, while int() truncates.
Example:
import math
negative_number = -4.7
print(math.floor(negative_number))
print(int(negative_number)) Output:
-5
-4I have executed the above example code and added the screenshot below.

In this case, math.floor() round down the negative number to -5, while int() truncates the decimal part and gives -4.
Check out Difference Between = and == in Python
3. Use the // Operator (Floor Division)
Python also provides the // operator, known as floor division or integer division. This operator divides the first argument by the second and rounds the result down to the nearest whole number, making it equivalent to the math.floor() function when used with positive numbers.
Example:
distance = 150.7
time = 2
speed = distance // time
print(f"Sarah's average speed from Los Angeles to San Francisco is: {speed} mph") Output:
Sarah's average speed from Los Angeles to San Francisco is: 75 mphIn this example, we calculate Sarah’s average speed by dividing the distance (150.7 miles) by the time (2 hours) using the // operator. The result is floored to 75 mph.
Read How to Write a List to a File in Python?
4. Floor Numbers in Numpy Arrays
When working with numerical data in Python, you often use the Numpy library. Numpy provides the numpy.floor() function to floor the elements of an array.
Example:
import numpy as np
prices = np.array([4.99, 2.75, 3.50, 1.99])
floored_prices = np.floor(prices)
print(f"The floored prices of the products are: {floored_prices}") Output:
The floored prices of the products are: [4. 2. 3. 1.]Here, we have an array of prices, and we use numpy.floor() to floor each element of the array.
Check out How to Select Items from a List in Python?
Applications of Flooring Numbers
Flooring numbers are useful in various real-world scenarios. Here are a few examples:
- Rounding down ages: When calculating age-related discounts or benefits, you often need to floor the age to the nearest integer.
import math
def calculate_senior_discount(age):
floored_age = math.floor(age)
if floored_age >= 65:
return 0.1 # 10% discount for seniors
else:
return 0
customer_age = 65.7
discount = calculate_senior_discount(customer_age)
print(f"Mr. Johnson's senior discount is: {discount * 100}%") Output:
Mr. Johnson's senior discount is: 10.0%- Calculating some complete items: When dealing with quantities that can only be whole numbers, flooring is useful.
import math
def calculate_complete_boxes(total_items, items_per_box):
complete_boxes = math.floor(total_items / items_per_box)
return complete_boxes
total_products = 47
products_per_box = 6
complete_boxes = calculate_complete_boxes(total_products, products_per_box)
print(f"The warehouse in Chicago can ship {complete_boxes} complete boxes.") Output:
The warehouse in Chicago can ship 7 complete boxes.- Displaying truncated values: When displaying numeric values with a limited number of decimal places, flooring can be used.
import math
def format_price(price):
return f"${math.floor(price * 100) / 100:.2f}"
product_price = 9.8734
formatted_price = format_price(product_price)
print(f"The price of the product in the New York store is: {formatted_price}") Output:
The price of the product in the New York store is: $9.87Read How to Check if a Tuple is Empty in Python?
Conclusion
In this tutorial, I helped you to learn how to floor a number in Python. We explored different ways to floor a number in Python. We learned about the math.floor() function, the int() function, and the // operator. We also discussed the difference between math.floor() and int() when dealing with negative numbers, how to floor numbers in Numpy arrays using numpy.floor() and some applications of flooring numbers.
You may also like to read:
- How to Concatenate Tuples in Python?
- How to Concat Dict in Python?
- How to Create an Empty Set in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.