How to round down a number in python

How to round down a number in python

Python is one of the most versatile and easy-to-use programming languages and one of its most powerful feature of it is the ability to perform mathematical operations with ease.

In this article, we will explore different methods to round down numbers in python with some examples.

Understanding Rounding Down a Number

Rounding down a number means taking a number and decreasing it to the nearest whole number which is less than the original number.

For example, if the number is 3.76, the round down result will be 3. If the number is 7.1, the round down result is 7. In other words, we can say that it discards the values after the decimal point and returns only the number that is to the left of the decimal point.

In python, we have a built-in round() function for performing most rounding operations but it does not allow us to round down to the nearest whole number.

However, we have different methods in python that we can use to perform round-down operations on a number.

Method 1: Round down number using floor division

Floor division (//) is a mathematical operator in python that returns the largest integer that is less than or equal to the result of a division.

Here, an example to perform a round down of a number using the floor division operator.

num = 10.8
round_down_num = num // 1
print(round_down_num) # output: 10

Here, we have divided 10.8 by 1 using the division operator (//) and it returned 10, which is the largest integer that is less than or equal to 10.8.

We have divided it by 1, because the result will always be equal to the number rounded down to the nearest integer.

Method 2: Using math.floor() function to round down in python

The math.floor() function in python takes a number as an argument and rounds the number down to the nearest integer and returns it.



Here is an example of how to use math.floor to round down a number:

import math;
num = 3.76
round_down_num = math.floor(num)

print(round_down_num) # 3 

In the example, we first imported the math library and then used the math.floor function on the number 3.67, which returned the round-down value i.e 3.

Method 3: Using int() function to round down a number

We can also use the int() function for rounding down a number in python. The int() function converts a specific number passed as an argument and converts it into an integer value.

When we pass a decimal value to int(), it rounds down the number to the nearest integer.

Here is an example to use int() to round down a number:

num = 3.76
round_down_num = int(num)

print(round_down_num) # output: 3

In the example, we have passed the number 3.76 to int() as an argument and it returns the output as 3.

Method 4: Using math.trunc() function

The math.trunc() method takes a number as an argument and returns the truncated integer part of the number.

We can use this trunc() method to round down a number in python. Here in an example:

import math
num = 3.76
round_down_num = math.trunc(num)

print(round_down_num) # output : 3

Here, we have imported the math library first and used math.trunc() function on the 3.76, which returned us the integer value i.e 3.


In this article, we have learned how to round down a number and control the precision of our calculation using four different methods, floor division, math.floor(), int(), and math.trunc() function in Python,


What is the difference between round() and math.floor() function in python.

The math.floor() function round down a floating point number to its nearest integer that is less than or equal to the original number.


import math
num = 3.65
print(math.floor(num) #3

Whereas the round() function rounds a floating point number to the nearest integer.

Let's take the same num as above in the example for round() method:

num = 3.65
print(round(num)) #4

As you can see, the difference in the output of the same number i.e 3.65, math.floor() gives us 3, whereas round() gives us 4.