There are two types of numbers in Python.
The integers are either positive numbers, negative numbers, or 0. Commonly referenced as int.
The floating-point numbers can contain decimals, whereas integers cannot. To convert a number stored in a string or integer convert into a floating-point number, use the float() function.
The math.floor() function in Python rounds a number down to the nearest integer and returns the result. Let’s see how to work with floating-values with floor() function.
Python floor float
To use a floor float in Python, use the int() function as long as your numbers are positive. The int() function can round down to the next integer. Then, the int() method on the float can be cast to that value to the lower int.
data = 3.1415161 rnd = int(data) print(rnd)
In this example, the int() function round down the 3.1415161 value to 3.
If you want the floating-point value output like 3.0 floating value, you can use the floor division(//) operator.
import math data = 3.1415161 rnd = data // 1 print(rnd)
You can see that we got the floating-point output.
Python floor float for negative numbers
If you use the floor division operator to the negative numbers, it gives the round-up value.
import math data = -3.1415161 rnd = data // 1 print(rnd)
That’s it for this tutorial.
Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms including Python. Krunal has written many programming blogs which showcases his vast knowledge in this field.