Python math.floor() - Floor of a Number
Python math.floor()
math.floor(x) function returns the largest integer less than or equal to x.
Syntax
The syntax to call floor() function is
math.floor(x)
where
Parameter | Required | Description |
---|---|---|
x | Yes | A numeric value. |
If x is infinity, then floor(x) raises OverflowError.
If x is nan, then floor(x) raises ValueError.
Examples
1. Find floor value of a float number
In the following program, we take a floating point number in x
, and find its floor value.
Python Program
import math
x = 3.14
result = math.floor(x)
print('floor(x) :', result)
Output
floor(x) : 3
2. Find floor value of a negative float number
In the following program, we take a negative floating point number in x
, and find its floor value.
Python Program
import math
x = -3.14
result = math.floor(x)
print('floor(x) :', result)
Output
floor(x) : -4
3. Find floor value of infinity
In the following program, we take the infinity constant in x
, and find its floor value.
Python Program
import math
x = math.inf
result = math.floor(x)
print('floor(x) :', result)
Output
OverflowError: cannot convert float infinity to integer
4. Find floor value of an integer
In the following program, we take an integer value in x
, and find its floor value.
Python Program
import math
x = 3
result = math.floor(x)
print('floor(x) :', result)
Output
floor(x) : 3
5. Find floor value of nan
In the following program, we take nan (not a number) constant in x
, and find its floor value.
Floor value of nan.
Python Program
import math
x = math.nan
result = math.floor(x)
print('floor(x) :', result)
Output
ValueError: cannot convert float NaN to integer
Summary
In this Python Math tutorial, we learned the syntax of, and examples for math.floor() function.