How to Round Off Decimal Numbers to Integer in Python

Round to nearest integer python; Through this tutorial, i am going to show you how to round off decimal numbers to integer in python.

How to Round Off Decimal Numbers to Integer in Python

You can use math.floor() function to round off decimal numbers to integer numbers.

Definition of Python Math.floor Function

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.

Round to nearest integer python

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

13

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

Recommended Python Tutorials

Leave a Comment