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:


Incompatibilities moving from Python 2 to Python 3:
* map
* The round function tie-breaking and return type

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
39 Incompatibilities moving from Python 2 to Python 3
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve