pythonpython-3.xpython-3.6rounding

Python Rounding "Properly"


For numbers like 10.5, round(10.5) gives 10, but round(11.5) gives 12. It seems as if five rounds to the closest even integer. According to the more mathematical, i.e. the one taught in most schools, if the digit is five, you should round up no matter what. How do you do this?


Solution

  • Python uses "banker's rounding" for x.5. Even numbers round down, odd numbers round up. You are saying it "should" round up, but that's just your definition. It's not the only definition.

    If you always want rounding up, do:

    result = int(x+0.5)