Python math.floor() function
The math.floor()
function in Python is used to return the largest integer that is less than or equal to a given number. This function is useful when you need to round a floating-point number down to the nearest whole number.
Syntax
x
: A numeric expression (integer or float) for which you want to find the floor value.
Return Value
- Returns the largest integer less than or equal to
x
, which is of typeint
.
Examples
Rounding down a positive float:
Rounding down a negative float:
Rounding an integer: If you pass an integer to
math.floor()
, it will return the integer itself.Rounding down a float that is already an integer:
Using
math.floor()
with complex expressions: You can usemath.floor()
in expressions:Rounding very small decimal numbers:
Summary
- The
math.floor()
function returns the largest integer less than or equal to the input value. - It works with both integers and floating-point numbers.
- The return type is always
int
. - This function is particularly useful in scenarios where you need to ensure that a value is rounded down, such as calculating the number of items that can fit into a fixed number of containers or pages.