How does quantize work for Decimal object? InvalidOperation: [<class 'decimal.InvalidOperation'>]

220 views Asked by At

So I'm having trouble understanding quantize for Decimal class. I've read the document and saw similar questions for my problem but for some reason, the solutions that were given did not work for me and I think I'm grossly misunderstanding what quantize even does. I thought it takes long digit float given as a Decimal and retains accuracy up to the precision I want but I'm not sure. I am trying to round up by 1 digit with precision and having worked with floats, I've noticed computation being off by a significant amount especially when dealing with a lot of digits. If anyone can help or explain what I'm missing, I'd greatly appreciate it.

This is just an example of one float I want to round.

from decimal import ROUND_HALF_UP, Decimal, getcontext

a = '9.688400490911022e+30'
# a is a numpy.float64

b = Decimal(a).quantize(Decimal('0.1'), rounding=ROUND_HALF_UP)


>>>InvalidOperation: [<class 'decimal.InvalidOperation'>]
0

There are 0 answers