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

C# string formatting with negative numbers are not working as expected in net5.0 #59570

Closed
bharatbodage opened this issue Sep 24, 2021 · 5 comments
Labels
area-System.Numerics question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@bharatbodage
Copy link

C# string formatting with negative numbers are not working as expected in net5.0

  • I have created .net 4.8 framework console app and net 5.0 console app.
  • Then I tried to set value.ToString(format) in both versions and they are giving different results
  • Example: var d = -0.0305441935; Console.WriteLine(d.ToString("f1"));
  • In .Net Framework 4.8 console app it gives me result => 0.0
  • whereas as in net 5.0 console app it gives me result => -0.0
  • Behavior in both should be same.
  • Look like its broken in net 5.0 version.
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Sep 24, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@stephentoub
Copy link
Member

cc: @tannergooding

@tannergooding
Copy link
Member

tannergooding commented Sep 24, 2021

This is as expected and fits the IEEE 754 requirements around parsing and formatting floating-point values: https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/

In particular, +0.0 and -0.0 are distinct values in the floating-point world and can impact the result of various operations. Since .NET Core 3, we have produced roundtrip able values by default which includes preserving the sign of negative zero.

If you would like to remove the sign, you will need to use a Custom numeric format string and in particular, you'll need to use the a 3 section format. I believe (but have not confirmed) that d.ToString("0.0;-0.0;0.0") should be roughly equivalent to the "f1" behavior of .NET Framework

  • The first section is for positive numbers and ensures the entire integral digit and 1 fractional digit is printed. The second section is for negative numbers and the third is for zero.

@tannergooding tannergooding added area-System.Numerics question Answer questions and provide assistance, not an issue with source code or documentation. and removed untriaged New issue has not been triaged by the area owner labels Sep 24, 2021
@ghost
Copy link

ghost commented Sep 24, 2021

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

C# string formatting with negative numbers are not working as expected in net5.0

  • I have created .net 4.8 framework console app and net 5.0 console app.
  • Then I tried to set value.ToString(format) in both versions and they are giving different results
  • Example: var d = -0.0305441935; Console.WriteLine(d.ToString("f1"));
  • In .Net Framework 4.8 console app it gives me result => 0.0
  • whereas as in net 5.0 console app it gives me result => -0.0
  • Behavior in both should be same.
  • Look like its broken in net 5.0 version.
Author: bharatbodage
Assignees: -
Labels:

question, area-System.Numerics

Milestone: -

@bharatbodage
Copy link
Author

bharatbodage commented Sep 28, 2021

@tannergooding Thanks for the explanations. This was very helpful.

@dotnet dotnet locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Numerics question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

3 participants