KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to read financial data and store it. The place I get the financial data from stores the data with incredible precision, however I am only interested in 5 figures after the decimal point. Therefore, I have decided to use t = .quantize(cdecimal.Decimal('.00001'), rounding=cdecimal.ROUND_UP) on the Decimal I create, but I keep getting an InvalidOperation exception. Why is this? >>> import cdecimal >>> c = cdecimal.getcontext() >>> c.prec = 5 >>> s = '45.2091000080109' >>> # s = '0.257585003972054' works! >>> t = cdecimal.Decimal(s).quantize(cdecimal.Decimal('.00001'), rounding=cdecimal.ROUND_UP) Traceback (most recent call last): File "<stdin>", line 1, in <module> cdecimal.InvalidOperation: [<class 'cdecimal.InvalidOperation'>] Why is there an invalid operation here? If I change the precision to 7 (or greater), it works. If I set s to be '0.257585003972054' instead of the original value, that also works! What is going on? Thanks!
Tags (comma-separated)
Save Edits
Cancel