You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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
The text was updated successfully, but these errors were encountered: