-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Get different result when using ToString("F2", new CultureInfo("de-DE") in .NET6 and .Framework4.7.2 #75588
Comments
Tagging subscribers to this area: @dotnet/area-system-globalization Issue DetailsDescriptioncode:
.dotnet6: output = 50.55 Reproduction Steps
Expected behavioroutput = 50.56 Actual behavioroutput = 50.55 Regression?No response Known WorkaroundsConsole.WriteLine(string.Format(new CultureInfo("de-DE"), "{0:0.00}", value)); ConfigurationNo response Other informationIs there any reason for this difference? Thanks!
|
That's a known breaking change, that happened in .NET Core 2.1
NB The logic behind this change is to make transformation number -> string -> number to preserve original value as much as possible, which is fine for serialization/deserialization scenarios. It's a major pain point for number -> string conversions for UI-related tasks though. |
@aloraman, in the net6.0 output "50.55", the least significant digit is 5, which is not even, so the text you quoted does not explain this difference. Instead, I think the cause is that (double)50.555 is a little less than the infinite-precision 50.555, so it is not halfway between 50.55 and 50.56. Even though it is halfway between (double)50.55 and (double)50.56. |
@KalleOlaviNiemitalo Well, yes. It happens because actual float-pointing value is less than 50.555, but it doesn't explain the difference with .NetFramework. It is indeed caused by that breaking change in .Net Core 2.1, but this quote is the only piece of documentation, that explains what happened. |
There was another change to floating-point number parsing and formatting in .NET Core 3.0: https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/#making-the-formatter-ieee-754-2008-compliant |
@Joe4evr |
IEEE 754 floating-point values can only exactly represent values that are a multiple of some power of 2. Since The floating-point parsing/formatting behavior prior to .NET Core 3.1 was not IEEE 754 compliant and had numerous edge case bugs where it would effectively "double round" due to not considering the exact represented value. The value rounded to 2 fractional digits is indeed The breaking changes here were intentional and documented as part of the move to be IEEE 754:2008 compliant and to bring ourselves inline with how other languages do floating-point parsing/formatting. This was documented in the blog post called out above (https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/#making-the-formatter-ieee-754-2008-compliant) and in the Breaking Changes documentation for .NET Core 3.0: https://docs.microsoft.com/en-us/dotnet/core/compatibility/3.0#floating-point-formatting-and-parsing-behavior-changed |
@guangzhengli I don't know what you are using double for, but please note that you should not use double for financial calculations. Use Decimal . |
Thanks for letting me know about these. |
Description
code:
.dotnet6: output = 50.55
.Framework4.7.2 output = 50.56
Reproduction Steps
Expected behavior
output = 50.56
Actual behavior
output = 50.55
Regression?
No response
Known Workarounds
Console.WriteLine(string.Format(new CultureInfo("de-DE"), "{0:0.00}", value));
Configuration
No response
Other information
Is there any reason for this difference? Thanks!
The text was updated successfully, but these errors were encountered: