Python “decimal” package gives wrong results

When you construct a Decimal from a floating-point number, you get the exact value of the floating-point number, which may not precisely match the decimal value because that’s how floating-point numbers work.

If you want to do precise decimal arithmetic, construct your Decimal objects from strings instead of floating-point numbers:

>>> Decimal('22.0') / Decimal('10.0') - Decimal('0.2')
Decimal('2.0')

Leave a Comment