Best Ways to Round Down Numbers in Python

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, number and ndigits. It returns the number rounded to ndigits. By default, the value of the ndigits parameter is zero.

Syntax:

round(number, ndigits)

Parameters:

  • 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.

Return Value:

  •  The number n rounded to ndigits.
print(round(12))
print(round(12.7))  
print(round(12.4))

OUTPUT:

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:

print(round(2.5))  
print(round(3.5))

OUTPUT:

2
4

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.

ValueTruncated ToResult
12.345Tens place10
12.345Ones place12
12.345Tenths place12.3
12.345Hundredths place12.34

Syntax:

math.trunc(x)

Parameter:

  • x -> The decimal number that needs to be truncated.

Return Value:

  • Truncated integer part of a number

Example:

import math
print( math.trunc(3.5) )
3

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.

ValueRounded Down ToResult
19.345Tens place10
19.345Ones place19
19.345Tenths place19.3
19.345Hundredths place19.34

Syntax:

math.floor(x)

Parameter:

  • x -> The decimal number that needs to be rounded down.

Return Value:

  • The number rounded down to nearest integer.

Example:

import math
print( math.floor(3.5) )
3

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.

Syntax:

int(valuebase)

Paramters:

  • value: number or string to be converted to int
  • base: number format

Example:

x = 1.3131
x = int(x)
print(x)
1

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. 

Syntax:

numpy.floor(a)

Parameter:

  • a: input array

Return Value:

  • array containing the floor value of every element

Example:

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)

Output:

-1

Conclusion:

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.

Happy Pythoning!

Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
name
name
9 months ago

I am trying to build the following function:
y=2+math.floor(x*0,25)
where my desired results for (x,y) would be
(1,2); (2,2); (3,2); (4,2); (5,3); and so on to have a “stairway graph”
the problem seems to be that the variable x is seen as a list that is filled with only one number and therefore cannot be operated in the desired way. Is there a workaround?

Pratik Kinage
Admin
8 months ago
Reply to  name

Yes, you can do that easily. I think your approach was little bit wrong.

import math
x = [1, 2, 3, 4, 5]
y = [2 + math.floor((i-1)*0.25) for i in x]
print(x, y)

This would return –
[1, 2, 3, 4, 5] [2, 2, 2, 2, 3]

Regards,
Pratik

John Doe
John Doe
5 months ago

To use math.trunc, this is the way that I have used:

import math

def rounddown(num, decimal_places):
	return math.trunc(num * 10**decimal_places) / 10**decimal_places
Last edited 5 months ago by John Doe
Farha
Farha
1 month ago

how to round down a number like 8.55489 into 8.5?

Pratik Kinage
Admin
1 month ago
Reply to  Farha

Use math.floor()