Python math.floor() Method

Python Math.floor

Python math.floor function is Math Function , which is available in Python math library.

This python math.floor function is used to return the closest integer value which is less than or equal to the specified expression or Value.

Python floor Function Syntax

The basic syntax of the python math floor Function is:

math.FLOOR (Expression)

In this post, we will show you, How to write floor Function in python programming with several examples.

Python Floor Function Example

The floor Function in Python is used to return the closest integer value which is less than or equal to given numeric value.

 # Python floor function example

import math

val = math.floor(12.95)
print(val) 

OUTPUT

15

In this Python floor example, we are finding the closest value of both positive and negative values.

 import math
 print('The final result of math.floor(11.45) = ', math.floor(11.45))
 print('The final result of math.floor(25.99) = ', math.floor(25.99))
 print()
 print('The final result of math.floor(-150.49) = ', math.floor(-150.49))
 print('The final result of math.floor(-170.99) = ', math.floor(-170.99))

OUTPUT

The final result of math.floor(11.45) =  11
The final result of math.floor(25.99) =  25

The final result of math.floor(-150.49) =  -151
The final result of math.floor(-170.99) =  -171

Python math.floor Function Example

The following query will show you the multiple ways to use this python floor function.

import math # This allows us to use the floor function
 a = 0.54
 b = -10.98
 c = 90.05
 d = 65.98
 e = math.floor(2.45 + 7.55 - 14.88)
 pi = math.floor(math.pi)
 x = [-22.45, 2.40, 9.65] # List Example
 y = (-2.45, 22.10, 22.95) # Tuple Example
 print(" The Value of 'a' after the floor function is: ", math.floor(a))
 print(" The Value of %.2f after the floor function is: %d" %(b, math.floor(b)))
 print(" The Value of %.2f after the floor function is: %d" %(c, math.floor(c)))
 print(" The Value of %.2f after the floor function is: %d" %(d, math.floor(d)))
 print(" The Value of 'e' after the floor function is: ", e)
 print(" The Value of 'PI' after the floor function is: ", pi)
 print("  ")
 printing List values
 print(" First Value from List is: ", math.floor(x[0]))
 print(" Second Value from List is: ", math.floor(x[1]))
 print(" Third Value from List is: ", math.floor(x[2]))
 print("  ")
 printing Tuple values
 print(" First Value from List is: ", math.floor(y[0]))
 print(" Second Value from List is: ", math.floor(y[1]))
 print(" Third Value from List is: ", math.floor(y[2]))

OUTPUT

 The Value of 'a' after the floor function is:  0
 The Value of -10.98 after the floor function is: -11
 The Value of 90.05 after the floor function is: 90
 The Value of 65.98 after the floor function is: 65
 The Value of 'e' after the floor function is:  -5
 The Value of 'PI' after the floor function is:  3

 First Value from List is:  -23
 Second Value from List is:  2
 Third Value from List is:  9

 First Value from List is:  -3
 Second Value from List is:  22
 Third Value from List is:  22

Difference between int and floor in Python

This is one of the frequently asked question because, both the int and floor functions return the same result for positive values. However, if you check with negative values then you can see the difference.

import math

print('math.floor(100.45) Result = ', math.floor(100.45))
print('math.floor(200.99) Result = ', math.floor(200.99))
print('int(100.45) Result = ', int(100.45))
print('int(200.99) Result = ', int(200.99))
print()
     
print('math.floor(-150.49) result = ', math.floor(-150.49))
print('math.floor(-260.99) result = ', math.floor(-260.99))
print('int(-150.49) result = ', int(-150.49))
print('int(-260.99) result = ', int(-260.99))

OUTPUT

math.floor(100.45) Result =  100
math.floor(200.99) Result =  200
int(100.45) Result =  100
int(200.99) Result =  200

math.floor(-150.49) result =  -151
math.floor(-260.99) result =  -261
int(-150.49) result =  -150
int(-260.99) result =  -260

Python floor Division Example

This operator return the floored result of the division.

# Python floor Division example

a = 100
b = 30

x = a / b
print(x)

y = a // b
print(y)

OUTPUT

3.3333333333333335
3

Python floor List Example

Math floor with List and For Loop

Use the math floor function of Python on Python List. In this example, we are using the For Loop to iterate list item and then applying the floor function for each item.

# Python math floor example

import math

numbers = [-15.99, 25.23, 45.67, -450.11, -650.91]

for num in numbers:
    print(math.floor(num))

OUTPUT

-16
25
45
-451
-651

Math Floor with Map and Lambda function

However, this time we are using the Map and Lambda function to iterate items.

import math

numbers = [-15.99, 25.23, 45.67, -450.11, -650.91]
print(numbers)
print()

floor_result = map(lambda num: math.floor(num), numbers)
print('The final result of math.floor = ', list(floor_result))

OUTPUT

[-15.99, 25.23, 45.67, -450.11, -650.91]

The final result of math.floor =  [-16, 25, 45, -451, -651]

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *