Python math.floor() Method

The math.floor() method returns the largest integer that is not greater than the input number.

If the input is already an integer, it returns that same number.

Syntax

import math
math.floor(x)

Parameters

x(required): It is a numeric number whose floor value you want to calculate.

Return value

It is the largest integer not greater than the input value.

Visual Representation

Visual Representation of Python math.floor() Method

Example 1: Passing positive numbers

# Import math library
import math

print(math.floor(300.9));
print(math.floor(10.2));
print(math.floor(7));

Output

300
10
7

Example 2: Passing negative numbers

For negative numbers, this function will round down (toward the smaller or more negative number), which means it moves away from zero.

Passing negative numbers

import math

print(math.floor(-300.9));
print(math.floor(-10.2));
print(math.floor(-7));

Output

-301
-11
-7

Example 3: Round down 2 decimal places

By multiplying the input number by 100, applying the math.floor() function, and then dividing the result by 100.0, you will get the floor value rounded down to 2 decimal places.

import math

number = 19.2110
print(math.floor(number * 100) / 100.0)

Output

19.21

Related posts

Python math.ceil()

Numpy.floor()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.