0

Python 3

>>> round(1.5)
2
>>> round(2.5)
2
>>> round(3.5)
4
>>> round(4.5)
4

I don't understand this. How is 3.5 rounded 4 but 4.5 rounded is not 5? Is there any way to get the normal way of round where if number is 5 or greater in the tenths column (>0.5) it will round to the next whole number?

EDIT: In Python 2.7 the round function behaved properly.

Answer - Credit: Ignacio Vazquez-Abrams.

To retain Python 2 rounding:

import math
>>> def old_round(n):
        return math.floor(n + 0.5)

>>> old_round(4.5)
5
Community
  • 1
  • 1
user1757703
  • 2,925
  • 6
  • 41
  • 62
  • Do you know how we get the old round behavior back in Python 3? – user1757703 Aug 06 '14 at 23:04
  • 2
    Add 0.5 and floor it. – Ignacio Vazquez-Abrams Aug 06 '14 at 23:04
  • `In Python 2.7 the round function behaved properly` According to who? [IEEE 754](http://en.wikipedia.org/wiki/Rounding#Round_half_to_even) does not agree with you. You can select rounding with the Decimal module if you wish. – dawg Aug 06 '14 at 23:18
  • @dawg Sorry, "properly" is wrong wording. I meant that it behaves as we are normally taught in school and life. If 0.5 or above then round up. Most people even today round 0.5 to the next number. – user1757703 Aug 06 '14 at 23:31

0 Answers0