-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Quantize with limited precision context #94
Comments
I think the problem here is that limitCon can't fit the original value with all its trailing zeros and so it fails. It's been a long time since I knew how this worked so it's hard for me to give a more detailed explanation. However you should consider using the Reduce method which removes trailing zeros. |
Well, problem with using |
I workarounded this by doing double parsing like this:
I have to make it a string and then parse again to control the ceiling value, which is not ideal but does what I need. It will return an |
You might try comparing how this works in java or python. It is possible they exhibit the same error cases. If so, this suggests that the exact method you are following is not covered by the common decimal spec. |
Yep, tried in Python, here is it:
Works as expected. And the overflow happens as expected as well:
|
I'm building a financial application that deals with multiple currencies and multiple Contexts with settings appropriate to each currency. The DB storage supports the widest possible range of values to permit to store any currency, DB storage uses Decimal(78,18), i.e.
MaxExponent=60
andMinExponent=-18
inapd
terms.I'm trying to quantize the value coming from DB with highest possible precision into the precision appropriate for given currency. The value is not large, but has many trailing decimal zeroes that I want to remove to bring it to correct precision.
And for whatever reason
Quantize()
does not produce the result I expect, but throws anOverflow
condition instead. Below is the sample code that reproduces it:In this piece of code the
wideCon
is the widest supported context in the system, i.e. the DB default context, whilelimitCon
is the target currency context. If I understand correctly the context settings - thelimitCon
effectively forces the values in this context to be within999999999.99999999
range, which is exactly what I need.The sample value is way below the
MaxExponent
limit, and I expectlimitCon.Quantize()
to produce same value with decimal places adjusted to fit, but instead it returns aNaN
value and anOverflow
condition.The error goes away if I increase the
MaxExponent
, it works atMaxExponent=12
for this value, but workarounding it this way means I will have to forfeit the ceiling check limit which I'd like to not do.What is a better way to achieve what I need - round an arbitrary long precision value to a constrained precision settings force-rounded per currency and with correct ceiling constraint?
The text was updated successfully, but these errors were encountered: