What is Python Floor?
floor() is a built-in math library function in Python that takes two numbers and returns the lowest of them.
The math.floor()
function takes a number as an argument and returns the largest integer that is not greater than the number given.
In addition to that, the math.floor()
function returns the closest integer value that is less than or equal to an expression or value.
To use the floor() method, you must import the Python math module.
Syntax
import math math.floor(x)
Arguments
The math.floor()
method takes x as a numeric number, a required parameter.
Example
Let’s look at the code below to see how the Python floor method works.
import math var1 = math.floor(300.72) print(var1)
First, we used the import statement to bring in the math library. It will let us use mathematical functions like the floor.
In the next line, we used the math.floor()
function and gave it 300.72 as an argument.
This gave us the output, which we stored in the “var1” variable and then showed in the Python console.
See the output:
300
Other Example
See the other example below.
# Import math library import math # Round numbers down to the nearest integer print(math.floor(0.6)) print(math.floor(1.4)) print(math.floor(5.3)) print(math.floor(-5.3)) print(math.floor(22.6)) print(math.floor(10.0))
Output:
0
1
5
-6
22
10
Python Floor Division
Floor division is an operation in Python that divides two numbers and rounds the result down to the nearest integer. The floor division occurs via the double-backslash (//) operator.
print(5//3)
Output:
1
When you do floor division, you divide and round down to the nearest whole number.
In the example above, rounding to the nearest integer is 1, so the output is also 1.
Python Floor to 2 Decimal Places
Use the math.floor()
function in Python to round down two (2) decimal places. Divide the result of math.floor()
by 100.0, and you’ll get the floor value with 2 decimal places.
import math
data = 19.2110
print(math.floor(data * 100) / 100.0)
Output:
19.21
Python Floor Function List Example
We will use this math function, “floor(),” on the items in the list.
Here, we’re using the For Loop to go through each item in the list, and then for each item, we’re using the floor function.
import math numbers = [-10.89, 20.22, 40.67, -350.11, -450.91] for num in numbers: print(math.floor(num))
Output:
-11
20
40
-351
-451
Difference Between Int and Floor
This is a frequently asked question because the int and floor functions both give the same answer when the value is positive. But you can see the difference if you check with a negative value as an argument.
import math print('math.floor(100.45) Result = ', math.floor(100.45)) print('math.floor(200.99) Result = ', math.floor(200.99)) print('int(100.45) Result = ', int(100.45)) print('int(200.99) Result = ', int(200.99)) print() print('math.floor(-150.49) result = ', math.floor(-150.49)) print('math.floor(-260.99) result = ', math.floor(-260.99)) print('int(-150.49) result = ', int(-150.49)) print('int(-260.99) result = ', int(-260.99))
Output:
math.floor(100.45) Result = 100
math.floor(200.99) Result = 200
int(100.45) Result = 100
int(200.99) Result = 200
math.floor(-150.49) result = -151
math.floor(-260.99) result = -261
int(-150.49) result = -150
int(-260.99) result = -260
Frequently Asked Questions
The math.floor()
method in Python rounds a number down to the nearest integer, if necessary, and returns the result.
It is one of the math functions available in the math library. In the same way, math.ceil() method in Python returns the ceiling value of the input value.
Math.floor()
in Python 3 gives an integer value. In Python, a common math function is to find the floor of a number.
The floor of a number is the nearest Python integer value that is less than or equal to the number.
The math.floor()
function always rounds down and returns the largest integer that is less than or equal to the given number.
The int and floor functions both give the same answer when the value is positive. But you can see the difference if you check with a negative value as an argument.
Conclusion
We have completely discussed the step-by-step guide to Floor Function In Python.
We learned this with the help of the different examples of floor() functions.
I hope this simple Python tutorial helped you a lot.
If you have any questions or suggestions about this article, please feel free to comment below. Thank You!