[issue24827] round(1.65, 1) return 1.6 with decimal

Merlijn van Deen report at bugs.python.org
Sat Aug 8 12:52:29 CEST 2015


Merlijn van Deen added the comment:

As Zachary explained, the behavior is correct. There are three issues in play here.

1) The rounding method. With the ROUND_HALF_EVEN rounding mode, .5 is rounded to the nearest *even* number, so 1.65 is rounded to 1.6, while 1.75 is rounded to 1.8.

2) Rounding of floats. Floats cannot represent every number, and numbers are therefore rounded.

 - round(2.675, 2) = round(2.6749999999999998, 2) and is thus rounded to 2.67
 - round(1.65, 1) = round(1.6499999999999999, 1) and is thus rounded to 1.6

3a) In Python 2, round returns a float, so Decimal(round(Decimal("1.65"))) = Decimal(1.6) =  Decimal('1.600000000000000088817841970012523233890533447265625') != Decimal('1.6')

3b) In Python 3, Decimal.__round__ is implemented, so round(D("1.65"), 1) == D("1.6") as expected.

----------
nosy: +valhallasw

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24827>
_______________________________________


More information about the Python-bugs-list mailing list