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

Conditional format specifier and negative zero #13712

Closed
Tracked by #47244
jeremybparagon opened this issue Nov 1, 2019 · 10 comments
Closed
Tracked by #47244

Conditional format specifier and negative zero #13712

jeremybparagon opened this issue Nov 1, 2019 · 10 comments
Assignees
Labels
area-System.Numerics bug Cost:S Work that requires one engineer up to 1 week help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@jeremybparagon
Copy link

I am looking for a way to make something like (-0.01).ToString("0.#;-0.#") produce "0". In .NET Core 3.0, it produces "-0". This makes the conditional format specifier unusable for me.

During my experimentation, I noticed that (-0.01).ToString("p0.#;n0.#") produces "-p0". Thus, it seems that the statement "negative values are always displayed without a minus sign when section separators are used" at https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--section-separator is not followed here.

@jkotas
Copy link
Member

jkotas commented Nov 1, 2019

cc @tannergooding

@tannergooding
Copy link
Member

The first issue described is by design. -0.0 and +0.0 are different values for the purposes of string parsing/formating (as per the IEEE 754 specification).

The second issue described is a bug, it should print n0

@tannergooding tannergooding self-assigned this Nov 1, 2019
@am11
Copy link
Member

am11 commented Nov 3, 2019

The second issue described is a bug, it should print n0

On .NET Core 2.2, Mono and .NET Framework 4.7.2, it prints p. On .NET Core 3.0, it prints -p.

The table produced by https://dotnetfiddle.net/vvAymM found some other differences:

- .NET Core 3.0
+ .NET Core 2.2, Mono and .NET Framework 4.7.2

-(-0.1).ToString("p#;n#"):               -p
-(-0.1).ToString("p0#;n0#"):             -p00
-(-0.1).ToString("p1#;n1#"):             -p1
+(-0.1).ToString("p#;n#"):               p
+(-0.1).ToString("p0#;n0#"):             p00
+(-0.1).ToString("p1#;n1#"):             p1

-(-0.01).ToString("p#;n#"):              -p
-(-0.01).ToString("p0#;n0#"):            -p00
-(-0.01).ToString("p1#;n1#"):            -p1
-(-0.01).ToString("p0.#;n0.#"):          -p0
-(-0.01).ToString("p1.#;n1.#"):          -p1
+(-0.01).ToString("p#;n#"):              p
+(-0.01).ToString("p0#;n0#"):            p00
+(-0.01).ToString("p1#;n1#"):            p1
+(-0.01).ToString("p0.#;n0.#"):          p0
+(-0.01).ToString("p1.#;n1.#"):          p1

@jeremybparagon
Copy link
Author

I realize now that I can use (-0.01).ToString("0.#;-0.#;0") to get what I wanted initially.

Regarding the second issue, would it make sense for the comments at https://github.com/dotnet/coreclr/blob/818b35978a1681690988b2c9c3f0b6a034d91e82/src/System.Private.CoreLib/shared/System/Number.Formatting.cs#L222-L225 and https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--section-separator to be updated if they are not going to be followed in .NET Core?

Thank you.

@tannergooding
Copy link
Member

Yes, the documentation should likely be updated as well.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 26, 2020
@tannergooding tannergooding added area-System.Numerics and removed area-System.Runtime untriaged New issue has not been triaged by the area owner labels Jun 23, 2020
@ghost
Copy link

ghost commented Jun 23, 2020

Tagging subscribers to this area: @tannergooding
Notify danmosemsft if you want to be subscribed.

@tannergooding tannergooding modified the milestones: 5.0.0, Future Jun 23, 2020
@tannergooding tannergooding added Cost:S Work that requires one engineer up to 1 week help wanted [up-for-grabs] Good issue for external contributors labels Jan 15, 2021
@huoyaoyuan
Copy link
Member

The documentation has already been updated:

Two sections The first section applies to positive values and zeros, and the second section applies to negative values.If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, the resulting zero is formatted according to the first section.

@jeremybparagon
Copy link
Author

@huoyaoyuan I see that the bug confirmed by @tannergooding is still present in .NET 7: (-0.01).ToString("p0.#;n0.#") still prints -p0. Should this issue remain open?

Also, when that is fixed, the documentation you quoted will not be correct. It says, "If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, the resulting zero is formatted according to the first section." But here we have -0.01 which will become zero after rounding but will be formatted according to the second section.

Thanks.

@huoyaoyuan
Copy link
Member

Sorry I didn't test this locally. Can you confirm the behavior in .NET 7? Could you also examine the expected behavior according to doc?

@jeremybparagon
Copy link
Author

@huoyaoyuan I tested it in .NET 7, and (-0.01).ToString("p0.#;n0.#") still outputs "-p0". This goes against the documentation that says, "If you want the final formatted value to have a minus sign, you should explicitly include the minus sign as part of the custom format specifier." Here I did not include a minus sign in the format specifier, but it is being added nonetheless.

The underlying issue is that the intended spec has changed as of .NET Core 3.0. Negative zero is now presented as -0 rather than 0. So the behavior specified by @tannergooding above as the intended behavior goes against the spec that says, "If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, the resulting zero is formatted according to the first section." The current behavior needs to change, and the documented behavior needs to change.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Numerics bug Cost:S Work that requires one engineer up to 1 week help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

7 participants