3

I am trying to use round function here. Sometimes it round down from .5 sometimes round up. So what's the problem?

Source file:

print("rounding up 0.5 is",round(.5))
print("rounding up 1.5 is",round(1.5))
print("rounding up 2.5 is",round(2.5))
print("rounding up 3.5 is",round(3.5))

Output:

rounding up 0.5 is 0
rounding up 1.5 is 2
rounding up 2.5 is 2
rounding up 3.5 is 4
Timofey Stolbov
  • 4,501
  • 3
  • 40
  • 45
Sean Klaus
  • 189
  • 2
  • 8

1 Answers1

5

From the docs:

if two multiples are equally close, rounding is done toward the even choice

So when you say rounding up, it's not necessarily rounding up. It's just rounding.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 1
    *"It's just rounding"* This specific technique (rounding to even integers when at *.5) is called ["banker's rounding"](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) – Cory Kramer May 12 '15 at 19:49