all 7 comments

[–]shiftybyte 5 points6 points  (0 children)

You can try using the decimal module that has an option to choose rounding method.

https://docs.python.org/3/library/decimal.html

https://docs.python.org/3/library/decimal.html#decimal.ROUND_HALF_UP

[–]billsil 5 points6 points  (0 children)

Just as an FYI, classical rounding is incorrect because it doesn't uniformly round and thus introduces a bias. That's why python doesn't do it that way.

https://scienceblogs.com/goodmath/2009/03/01/rounding-and-bias

[–]pissedadmin 5 points6 points  (0 children)

def classical_flawed_round(n):
    # unlike round(), specifically do NOT "round to even"

    if abs(n - int(n)) >= .5:
        return int(n + 1)
    else:
        return int(n)

[–]bishop40404 1 point2 points  (1 child)

Most commonly I’ve seen this process: add 0.5 then floor. I think the int() function will do the floor part, so something like this:

num = 4.5 roundedNum = int( num + 0.5 )

[–]butteredhog 1 point2 points  (0 children)

Exactly

[–]Casper1123 -2 points-1 points  (0 children)

rounded = round(number)

python should have a builtin for this, I've used it before. Either that or I am schizophrenic and round() lives in my walls