Skip to content
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

Constructor from decimal truncates to 12 decimal places #66

Open
NullQubit opened this issue Dec 21, 2024 · 1 comment
Open

Constructor from decimal truncates to 12 decimal places #66

NullQubit opened this issue Dec 21, 2024 · 1 comment
Assignees
Labels

Comments

@NullQubit
Copy link

NullQubit commented Dec 21, 2024

Description
So I spent a few hours tracking down where some inaccuracies were coming from and ended up at this:
new BigDecimal(79193.074346525013163981460149m)

Digging into it a bit deeper I noticed that the library uses ToString("G17") to parse the decimal as a string. G17 format should not be used for decimals as some accuracy is lost.

Error/Exception Message
When instantiating using the BigDecimal(decimal) constructor with the above value, the resulting BigDecimal truncates the value to 12 decimal places (17 total significant digits) Example:

new BigDecimal(79193.074346525013163981460149m) + 0.0000000000009m

Note: The truncation happens also when a cast (implicit or explicit) is performed to a decimal.

Expected Behavior
The result should be a BigDecimal with value 79193.074346525014063981460149

Actual Behavior
The resulting value is 79193.0743465250139 because after the constructor is called the value was truncated to 79193.074346525013

Steps To Reproduce
Instantiate using the BigDecimal(decimal) constructor and pass a decimal with more than 17 significant digits

@AdamWhiteHat
Copy link
Owner

Kay, that should be fixed.
I published a new NuGet version that includes this fix: ExtendedNumerics.BigDecimal 3000.0.2.358

The main issue was I was funneling all type conversions through the string parsing function, which would be fine, except there was a known issue with .NET numeric string formatting where full precision and roundtripping between string formatting and parsing was not guaranteed, and it did not sound like Microsoft was intent on fixing it.
Fixing this required obtaining the Decimal value through numeric methods instead of relying upon consistent and correct string formatting from the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants