Skip to content
Advertisement

Behaviour of ROUND_HALF_DOWN , Python 3.9.5, decimal module

OS: fedora 34 workstation, GNU/Linux

I am confused. In the decimal module documentation section “Rounding modes” says,

decimal.ROUND_HALF_DOWN

    Round to nearest with ties going towards zero.

I have an example

import decimal
from decimal import Decimal, getcontext

print('----')
getcontext().prec = 4
getcontext().rounding = decimal.ROUND_HALF_DOWN
print(getcontext())
n1 = Decimal('25.2525') + Decimal('0.003')
print(n1)

I was expecting as result 25.25 because the sum results in n1 = 25.2555 has a 5 in the thousandths position, then must go DOWN to 0 and the hundredths position remain the same.

Am I wrong?

why I am getting there 25.26?

enter image description here enter image description here

Advertisement

Answer

It says:

Round to nearest with ties going towards zero.

In this particular case there isn’t a tie because 25.2555 is closer to 25.26 than it is to 25.25. So it just rounds to the nearest, which is 25.26.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement