Rounding the number is used in programming to perform complex calculations. Numbers are either rounded up or rounded down according to the algorithm condition. To round down a number various functions are used in Python such as math.floor(), int(), trunc(), etc.
The below methods are discussed in this post using various examples:
- Method 1: Using math.floor() Function
- Method 2: Using int() Function
- Method 3: Using trunc() Function
- Method 4: Using // Operator
- Method 5: Using numpy.floor() Function
Method 1: Using math.floor() Function
The “math.floor()” function is derived from the math module that rounds down the number to the nearest integer.
Here is an example code:
Code:
import math
print(math.floor(52.25))
print(math.floor(1.9))
print(math.floor(-5.5))
The “math.floor()” function takes the positive and negative float as an argument and returns the value by rounding down a decimal to the nearest integer.
Output:
The input numbers have been rounded down.
Method 2: Using int() Function
The inbuilt “int()” function is used to convert the float numbers or strings into equivalent integers. Here, is an example of how we can use this function to round down a number:
Code:
print(int(55.9))
print(int(9.354))
print(int(-9.5))
The inbuilt “int()” function takes the positive and negative float number and round down to the nearest integers.
Output:
The given numbers have been rounded down.
Method 3: Using trunc() Function
The “trunc()” function of the math module is used to eliminate the fractional part of the number and retrieve an integer.
The below code uses this function to round a number down:
Code:
import math
print(math.trunc(4.35))
print(math.trunc( 9.95))
print(math.trunc(-4.32))
print(math.trunc( -9.65))
The “math.trunc()” function takes the positive and negative float as a parameter and truncates the fractional part of the value.
Output:
The input numbers have been rounded down.
Method 4: Using // Operator
The floor division can also be performed using the “//” operator in Python. The below code uses the “//” operator to round down a number:
Code:
print('Without Rounding Down: ', 47/2)
print('After Rounding Down: ',47//2)
The “//” operator divides the numerator “47” by the denominator “2” and rounds the output/results down to the nearest integer.
Output:
The division has been rounded down to the closest integer.
Method 5: Using numpy.floor() Function
The “numpy.floor()” function of the numpy module is also used to round down a float to the nearest integers. Here is an example to help you understand:
Code:
import numpy
print(numpy.floor(55.55))
print(numpy.floor(2.99))
print(numpy.floor(-5.1))
The “numpy.floor()” function takes the float and rounds it down to the nearest integer.
Output:
The given float numbers have been rounded down.
Conclusion
To round a number down the “math.floor()” function, “int()” function, “trunc()” function, “// operator”, and “numpy.floor()” function is used in Python. The “math.floor()”, “int()”, “trunc()” and other functions accept positive and negative float values as an argument and round them down to the nearest integer. The “//” operator also performs the floor division.
This Python guide has provided numerous methods for rounding down numbers with examples.