0

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')

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.