Decimal ROUND_HALF_EVEN Default

Raymond Hettinger python at rcn.com
Tue Jan 17 05:36:12 CET 2006


LordLaraby wrote:
> If 'bankers rounding' is HALF_ROUND_EVEN, what is HALF_ROUND_UP? I
> confess to never having heard the terms.

The terms are defined in the docs for the Context object:
    http://docs.python.org/lib/decimal-decimal.html

The rounding option is one of:
------------------------------
ROUND_CEILING (towards Infinity),
ROUND_DOWN (towards zero),
ROUND_FLOOR (towards -Infinity),
ROUND_HALF_DOWN (to nearest with ties going towards zero),
ROUND_HALF_EVEN (to nearest with ties going to nearest even integer),
ROUND_HALF_UP (to nearest with ties going away from zero), or
ROUND_UP (away from zero).


> I usually do: Y = int(X + 0.5) scaled to proper # of decimal places.
> Which type of rounding is this? If either.

The interpreter shows that ties are rounded down towards zero:

>>> [int(x + 0.5) for x in range(-5, 6)]
[-4, -3, -2, -1, 0, 0, 1, 2, 3, 4, 5]


Raymond




More information about the Python-list mailing list