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
{{ message }}
Repository navigation
Conditional format specifier and negative zero #13712
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.
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
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.
@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.
@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 @tannergoodingabove 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 locked as resolved and limited conversation to collaborators on Apr 14, 2023
Activity
jkotas commentedon Nov 1, 2019
cc @tannergooding
tannergooding commentedon Nov 1, 2019
The first issue described is by design.
-0.0and+0.0are 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
n0am11 commentedon Nov 3, 2019
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:
jeremybparagon commentedon Nov 4, 2019
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 commentedon Nov 4, 2019
Yes, the documentation should likely be updated as well.
huoyaoyuan commentedon Mar 11, 2023
The documentation has already been updated:
jeremybparagon commentedon Mar 13, 2023
@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.01which will become zero after rounding but will be formatted according to the second section.Thanks.
huoyaoyuan commentedon Mar 15, 2023
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 commentedon Mar 15, 2023
@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
-0rather than0. 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.