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?
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.