Does Python have floor? [Explain With Example]

Yes, Python has a built-in function called floor() that is part of the math module. The floor() function is used to round a floating-point number down to the nearest integer that is less than or equal to the original number. This is often referred to as “floor division” or “floor rounding”.

Here’s an example of how to use the floor() function:

import math

number = 5.7
result = math.floor(number)

print(result)  # Output: 5
Code language: Python (python)

In this example, the math.floor() function is used to round the floating-point number 5.7 down to the nearest integer, which is 5.

What is floor and ceil in Python?


The opposite of the math.floor() function in Python is the math.ceil() function. The math.ceil() function is also part of the math module and is used to round a floating-point number up to the nearest integer that is greater than or equal to the original number. This operation is commonly known as “ceiling division” or “ceiling rounding”.

Here’s an example of how to use the math.ceil() function:

import math

number = 5.7
result = math.ceil(number)

print(result)  # Output: 6
Code language: Python (python)

In this example, the math.ceil() function is used to round the floating-point number 5.7 up to the nearest integer, which is 6.

Read More;

  • Muhammad Nabil

    I am a skilled and experienced Python developer with a huge passion for programming and a keen eye for details. I earned a Bachelor's degree in Computer Engineering in 2019 from the Modern Academy for Engineering and Technology. I am passionate about helping programmers write better Python code, and I am confident that I can make a significant contribution to any team. I am also a creative thinker who can come up with new and innovative ways to improve the efficiency and readability of code. My specialization includes Python, Django, SQL, Apache NiFi, Apache Hadoop, AWS, and Linux (CentOS and Ubuntu). Besides my passion for Python, I am a solo traveler who loves Pink Floyd, online video games, and Italian pizza.

    View all posts

Leave a Comment