Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:39:03+00:00 2026-05-31T06:39:03+00:00

I am trying to read financial data and store it. The place I get

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:39:04+00:00Added an answer on May 31, 2026 at 6:39 am

    decimal version gives a better description of the error:

    Python 2.7.2+ (default, Feb 16 2012, 18:47:58) 
    >>> import decimal
    >>> s = '45.2091000080109'
    >>> decimal.getcontext().prec = 5
    >>> decimal.Decimal(s).quantize(decimal.Decimal('.00001'), rounding=decimal.ROUND_UP)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/decimal.py", line 2464, in quantize
        'quantize result has too many digits for current context')
      File "/usr/lib/python2.7/decimal.py", line 3866, in _raise_error
        raise error(explanation)
    decimal.InvalidOperation: quantize result has too many digits for current context
    >>> 
    

    Docs:

    Unlike other operations, if the length of the coefficient after the
    quantize operation would be greater than precision, then an
    InvalidOperation is signaled. This guarantees that, unless there is an
    error condition, the quantized exponent is always equal to that of the
    right-hand operand.

    But i must confess i don’t know what this means.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read xml string from DB. However my code is erroring?
I trying to read a value from an ArrayCollection, I use getItemAt and get
I'm trying to read through a gmail account to get gps data that is
I m trying read XML data using XML parser from Url( https://....etc ). But
Im trying to read some data from a binary file into a buffer allocated
I am trying to read data using the following code from a socket: n
Iam trying to read data from a table. Initially it is empty. If i
Im trying to read a column of String values from my DB. Im trying
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
I am trying to read ASCII text response from a tcp open streaming socket

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.