Skip to content

Conditional format specifier and negative zero #13712

Closed

Description

@jeremybparagon

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.

Activity

  1. jkotas commented on Nov 1, 2019

    @jkotas
    Member
  2. tannergooding commented on Nov 1, 2019

    @tannergooding
    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

  3. self-assigned this
    on Nov 1, 2019
  4. am11 commented on Nov 3, 2019

    @am11
    Member

    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
  5. jeremybparagon commented on Nov 4, 2019

    @jeremybparagon
    Author
  6. tannergooding commented on Nov 4, 2019

    @tannergooding
    Member

    Yes, the documentation should likely be updated as well.

  7. transferred this issue fromdotnet/coreclron Jan 31, 2020
  8. added this to the 5.0 milestone on Jan 31, 2020
  9. added
    untriaged
    on Feb 26, 2020
  10. added and removed
    untriaged
    on Jun 23, 2020
  11. modified the milestones: 5.0.0, Future on Jun 23, 2020
  12. added
    Cost:S
    help wanted
    on Jan 15, 2021
  13. huoyaoyuan commented on Mar 11, 2023

    @huoyaoyuan
    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.
  14. jeremybparagon commented on Mar 13, 2023

    @jeremybparagon
    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.

  15. huoyaoyuan commented on Mar 15, 2023

    @huoyaoyuan
    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?

  16. jeremybparagon commented on Mar 15, 2023

    @jeremybparagon
    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.

  17. ghost locked as resolved and limited conversation to collaborators on Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Cost:Sarea-System.Numericsbughelp wanted

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions