-
Notifications
You must be signed in to change notification settings - Fork 5k
Custom Numeric Format String Rounds Erroneously #114104
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
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics |
This isn't erroneous, the values you typed for x1/x2 aren't the actual values. That is, for all floating-point values ( So you typed:
This is then compiled down such that the actual value is: double x1 = 28.599999999999798916405779891647398471832275390625;
double x2 = 28.5999999999999801048033987171947956085205078125; Because these are the representable values that are nearest to the literal you typed. You then gave a custom format string of Matching that against the actual represented values we get:
Now, for historical reasons, custom format strings for binary floating-point values support printing no more than 15 total significant digits (this is partially covered under https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/). You've specified 14
This means that the first number has the last digit of As per the blog post, there would be significantly more work required to support custom numeric format strings with more than 15 significant digits (for |
This issue has been marked |
#113155 ? Update: looks unrelated |
This issue has been automatically marked |
Uh oh!
There was an error while loading. Please reload this page.
Description
double
precision floating point values appear to get erroneously rounded by custom format strings using#
when they exceed a threshold of decimal precision.It appears to occur when we exceed 13 digits of decimal precision (to the right of the decimal point). For example, given the custom format string
0.##############
('up to fourteen decimal digits'), the value28.5999999999998
(thirteen decimal digits) will render as a string correctly, but28.59999999999998
(fourteen decimal digits) will be rendered as28.6
.Reproduction Steps
var x1 = 28.5999999999998d;
var x2 = 28.59999999999998d;
Console.WriteLine($"{x1:0.##############}"); // "28.5999999999998"
Console.WriteLine($"{x2:0.##############}"); // "28.6"
Expected behavior
As long as the number of
#
to the right of the decimal meets or exceeds the number of decimal digits in the value, the full exact decimal value should be preserved in the string output.Actual behavior
When the decimal digits exceeds thirteen, the custom string format executes erroneous rounding against the developer's request, and without offering e.g. an error or exception regarding an invalid format string.
Regression?
Tested also with Framework 4.8.1 and the same behavior is present, so this does not appear to be a regression.
Known Workarounds
No response
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: