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

ParameterRequiredDescription
xYesA numeric value.

If x is infinity, then floor(x) raises OverflowError.

If x is nan, then floor(x) raises ValueError.

Examples

Floor value of floating point number.

Python Program

import math

x = 3.14
result = math.floor(x)
print('floor(x) :', result)
Run

Output

floor(x) : 3

Floor value of negative floating point number

Python Program

import math

x = -3.14
result = math.floor(x)
print('floor(x) :', result)
Run

Output

floor(x) : -4

Floor value of infinity.

Python Program

import math

x = math.inf
result = math.floor(x)
print('floor(x) :', result)
Run

Output

OverflowError: cannot convert float infinity to integer

Floor value of an integer.

Python Program

import math

x = 3
result = math.floor(x)
print('floor(x) :', result)
Run

Output

floor(x) : 3

Floor value of nan.

Python Program

import math

x = math.nan
result = math.floor(x)
print('floor(x) :', result)
Run

Output

ValueError: cannot convert float NaN to integer

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.floor() function.