Sitemap

Diving into python round() function

3 min readFeb 19, 2025

--

Press enter or click to view image in full size

Introduction

In today’s world, where accuracy is crucial, the round() function in Python plays a vital role. This is especially true when working with numbers from banks or financial institutions, where precise calculations are essential for decision-making. In these scenarios, rounding numbers can have a significant impact. Python simplifies this process with the round() function, making it easier to handle. Let’s explore how this function works in detail.

Understanding basics of round() function

defination — round(number, n_digits=none)

number is the value which needs to be round. This parameter is mandatory for function.

n_digits is the number of decimals you want to keep. This is optional parameter to function.

e.g. round(3.6) = 4

Diving deep into round() function

When we give number of decimals(ndigits) in round() function, values are rounded to nearest multiple of 10^-ndigits. if ndigits are omitted or its none , round() function returns nearest integer to its input.

Let’s understand above statement with example — round(3.1435,2)

When we give decimal values are rounded to nearest multiple of 10^-ndigits. i.e. 10^-2 = 1/100 = 0.01

Now what is nearest multiple of 0.01 for number — 3.1435?

There are two possibilities 3.14 or 3.15 but 3.14 is nearest to 3.1435 hence the answer is 3.14.

So behaviour of round function changes based on the value of ndigits.

  1. ndigits=0

if ndigit value is not given the number is rounded off to nearest integer. round(1.6)=2

Get Akshay Kudale’s stories in your inbox

Join Medium for free to get updates from this writer.

2. ndigits>0

If ndigit value is greater than 0, the number is rounded off to nearest multiple of 10^-ndigits.

round(3.1436) = 3.14

3. ndigits<0

It is just opposite off when ndigits>0. e.g. round(878.77,-1) will round off to 880 since we need nearest multiple of 10¹ i.e 10. In case of round(878.77,-2), we need nearest multiple of 100 which is 900.

Secrets of round() function

Till now, round() function seems to be straightforward but there are multiple tricks or secrets of it.

  1. We have already seen that, when we give ndigit values are rounded off to nearest multiple of 10^ndigits. But there is something more to it. If two multiples are equally close, rounding is done towards even choice.

e.g. round(2.5)=2, round(0.5)=0

This is also known as bankers rounding or round to even.

2. The behaviour of round function is surprising sometimes in case of floats.

e.g. round(2.675,2) gives 2.67. Ideally it should give 2.68 according whatever we discussed above. Let’s understand why this happens?

Floating point numbers are reperesented in computer hardware as binary fractions. The most of the decimal fractions can’t be represented exactly in binary so numbers like 2.675 can’t be stored with perfect precision, leading to slight errors when they are reperesented internally.

Let’s understand this in detail:

The internal binary representation of 2.675 would be something like 2.674999999999999822364316059… this is approximation beacuse decimal can’t be exactly stored as binary. Now if we see this approximation it is closed to 2.67 rather than 2.68. Hence the result is 2.67.

Conclusion

So the way we round number may be little trickier but its important in application where precision matters. So do deep dive into this, try it in your code editor and round off the numbers with confidence😊

--

--