Hello coders!! In this article, we will be learning the different ways to round down a number in Python. The rounding of a number is basically making the number simpler while keeping its value close to what it was. Let us see how we can round down a number in Python.
Inbuilt round() function in Python:
In Python, we are provided with a built-in
round() function. This function takes two numeric parameters,
ndigits. It returns the number rounded to
ndigits. By default, the value of the
ndigits parameter is zero.
- number – the number which needs to be rounded
- ndigits (optional) – The value up to which the given number needs to be rounded. Its default value is 0.
- The number
print(round(12)) print(round(12.7)) print(round(12.4))
12 13 12
We have not passed any value to the ndigits parameter in this example. As a result, it takes the default value, i.e., 0. We can see that the integer number remains at it is, 12.7 is rounded up to 13, and 12.4 is rounded down to 12.
Understanding the problem with round() method:
As we can see that 2.5 is rounded down to 2, whereas 3.5 is rounded up to 4. Well, this is no glitch in Python programming. It’s just how the round() function works. Let us explore some different ways to round down an integer in Python.
Different ways to round down a number in Python:
We will discuss 5 other methods to round down a number in python except the round() method.
1) Truncation Method to Round down in Python:
As the name suggests, truncation is used to shorten things. It is a straightforward method that can be used to round a number by truncating a given number of digits.
- x -> The decimal number that needs to be truncated.
- Truncated integer part of a number
import math print( math.trunc(3.5) )
In this example, we have used inbuilt math.trunc() method of Python from the math module to obtain the integer part of the given decimal number.
2) Using math.floor():
A number can be rounded up to a certain digit.
|Value||Rounded Down To||Result|
- x -> The decimal number that needs to be rounded down.
- The number rounded down to nearest integer.
import math print( math.floor(3.5) )
As we can see in this example, the number is rounded up to the nearest integer greater than the number itself.
3) Using int() to round down a number in python:
This method is essentially used to convert the given number into integer.
- value: number or string to be converted to int
- base: number format
x = 1.3131 x = int(x) print(x)
Here, we used the int() method to convert the decimal number into an integer, thus rounding it down to the nearest possible integer value.
4) Using numpy.floor() to round down:
It is a mathematical function that returns the floor of the elements of array.
- a: input array
- array containing the floor value of every element
import numpy as np x = np.array([1.22, 1.67, 2.53]) x = np.floor(x) print(x)
[1. 1. 2.]
As you can see, using the numpy.floor() method, we were able to round down the elements of a given array.
5) Using // operator to round down in Python:
// is the floor division operator of Python. It returns floor value for both integer and floating-point arguments.
x = 1.3131 x = x // 1 print(x)
With this, we come to an end with this article. These are the two ways the number can be rounded down in Python. One can also round down a number using math.ceil() method.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.