I am using Python 3.5.3 and I have a strange rounding behavior
round(1.5)
Out[16]: 2
round(2.5)
Out[17]: 2
round(3.5)
Out[18]: 4
Why wasn't round(2.5) rounding to 3?
I am using Python 3.5.3 and I have a strange rounding behavior
round(1.5)
Out[16]: 2
round(2.5)
Out[17]: 2
round(3.5)
Out[18]: 4
Why wasn't round(2.5) rounding to 3?
The Python Documentation says:
If two multiples are equally close, rounding is done toward the even choice (so, for example, both
round(0.5)
andround(-0.5)
are 0, andround(1.5)
is2
).
If you want to be able to round toward positive infinity if it is in the middle, try this:
import math
def round(number):
if (math.ceil(number) - number >= number - math.floor(number)):
return math.ceil(number)
else:
return math.floor(number)
Testable at: https://repl.it/KHJY