How to round float 0.5 up to 1.0, while still rounding 0.45 to 0.0, as the usual school rounding?

16,320

Solution 1

It is actually currently considered proper to NOT blindly round *.5 up. Rather, it is proper to round *.5 to the nearest even number. Python 3 implements this "proper" form of "banker rounding", but a lot of other languages don't (yet). Blindly rounding *.5 up produces a slight bias, but "banker rounding" helps to balance it it out. See this thread for more info. So...

Method 1

You could conditionally use aceil(...) function (from the math module for the rounding up aspect. You'll have to do it conditionally in order to also maintain the regular rounding behavior for values less than 0.5. Try something like the following (note that this isn't extremely robust in that it only works on positive values...it should be able to be easily adapted to work with both positive and negative values though):

import math

val = 1.5
x = 0

if (float(val) % 1) >= 0.5:
    x = math.ceil(val)
else:
    x = round(val)

Note that a ceil(...) function will return an integer, not a float. This shouldn't be a major issue, but now you are aware.

Method 2

From the post I linked to above, it looks like another option is to use the decimal module to emulate the "old" way of rounding's behavior. I'm kind of copy & pasting from there, but here you go:

import decimal

x = decimal.Decimal('1.5').quantize(decimal.Decimal('1'), 
rounding=decimal.ROUND_HALF_UP)

Supposedly the decimal.ROUND_HALF_UP form of rounding is what you are looking for. This way you don't have to use a ceil(...) function conditionally.

I'm guessing that this was marked as a duplicate of another because a little digging would have given you more than enough info on this topic. (I didn't mark it as a duplicate, I'm just assuming that is why someone else did.)

Solution 2

Getting the "school" rounding, with rounding away from 0 for value in between, also for negative numbers, the function below can be used. This is also the rounding that was in Python 2.

def round_school(x):
    i, f = divmod(x, 1)
    return int(i + ((f >= 0.5) if (x > 0) else (f > 0.5)))

Some example results:

 1.50:  2
 1.49:  1

 0.50:  1
 0.49:  0

-0.49:  0
-0.50: -1

-1.49: -1
-1.50: -2
Share:
16,320
EquipDev
Author by

EquipDev

Updated on June 04, 2022

Comments

  • EquipDev
    EquipDev almost 2 years

    Appears that the default Python round(1 / 2) gives 0.

    How to round float 0.5 up to 1.0, while still rounding 0.45 to 0.0, as the usual school rounding?

    NOTE: Sorry for deleting and posting this again, but it was incorrectly marked as duplicate of another question.

  • Luke Hollenback
    Luke Hollenback about 7 years
    @StefanPochmann I edited the first paragraph of this post to explain a little better. It isn't that it always rounds up. Instead, it is that it rounds *.5 to the nearest even number. So yes, round(1.5) == 2. But also, round(2.5) == 2.
  • Vojtech Stas
    Vojtech Stas over 2 years
    I have found this post more usefull then the accepted post. It seems more clear and you dont have to import any other libs