Decimal Python Library has weird behavior

Question:

I’m trying to fix all my calculation by using Python Decimal. I’m limiting the decimal places to 3, nevertheless, I’m getting this behavior:

from decimal import *
getcontext().prec = 3

In [0]:Decimal('32.983') - Decimal('0.000')
Out[1]: Decimal('33.0')

Does anyone know how to overcome that? I would like to get:

Out[1]: Decimal('32.983')

Asked By: João Paina

||

Answers:

If you want to get 32.983, then you would need to set the precision to 5, not 3. Precision is the count of significant digits, not the count of digits after the decimal point.

You shouldn’t normally put a drastic limit on the precision of intermediate computations, since that will affect the accuracy of the final result. You can always round the final result for display.

Answered By: rici