The round function tie-breaking and return type

suggest change

round() tie breaking

In Python 2, using round() on a number equally close to two integers will return the one furthest from 0. For example:

round(1.5)  # Out: 2.0
round(0.5)  # Out: 1.0
round(-0.5)  # Out: -1.0
round(-1.5)  # Out: -2.0

In Python 3 however, round() will return the even integer (aka bankers’ rounding). For example:

round(1.5)  # Out: 2
round(0.5)  # Out: 0
round(-0.5)  # Out: 0
round(-1.5)  # Out: -2

The round() function follows the half to even rounding strategy that will round half-way numbers to the nearest even integer (for example, round(2.5) now returns 2 rather than 3.0).

As per reference in Wikipedia, this is also known as unbiased rounding, convergent rounding, statistician’s rounding, Dutch rounding, Gaussian rounding, or odd-even rounding.

Half to even rounding is part of the IEEE 754 standard and it’s also the default rounding mode in Microsoft’s .NET.

This rounding strategy tends to reduce the total rounding error. Since on average the amount of numbers that are rounded up is the same as the amount of numbers that are rounded down, rounding errors cancel out. Other rounding methods instead tend to have an upwards or downwards bias in the average error.

round() return type

The round() function returns a float type in Python 2.7

round(4.8)
# 5.0

Starting from Python 3.0, if the second argument (number of digits) is omitted, it returns an int.

round(4.8)
# 5

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents