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

Zachary Ware report at bugs.python.org
Sat Aug 8 05:13:53 CEST 2015


Zachary Ware added the comment:

The rounding mode of the default context is ROUND_HALF_EVEN[1]:

>>> import decimal
>>> decimal.getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])

For your examples near the end, see [2]:

>>> round(2.675, 2)
2.67
>>> round(decimal.Decimal('2.675'), 2)
Decimal('2.68')
>>> decimal.Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')
>>> round(_, 2)
Decimal('2.67')

[1] https://docs.python.org/3/library/decimal.html#decimal.ROUND_HALF_EVEN
[2] https://docs.python.org/3/tutorial/floatingpoint.html

----------
nosy: +zach.ware
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

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


More information about the Python-bugs-list mailing list