Skip to content

Negative numeric parsing inconsistencies / can't parse standard Windows format option #101873

Description

@mikernet

Description

There are inconsistencies with how NumberStyles.AllowLeadingWhite and AllowTrailingWhite behave. Incorrect handling of the former means that if a user has their system set to use this standard available format in Windows, numbers can never be parsed back from that string representation:

image

This seems rather problematic, i.e. if an application is displaying numeric values in a textbox using the user's preferred format, which is typically what is done, then it will fail to convert that value back to its numeric type.

Reproduction Steps

var style = NumberStyles.AllowLeadingWhite |
    NumberStyles.AllowTrailingWhite |
    NumberStyles.AllowLeadingSign |
    NumberStyles.AllowTrailingSign;

int.Parse("123 -", style) // Ok with AllowTrailingWhite
int.Parse("- 123", style) // Always throws, even with AllowLeadingWhite 

This next inconsistency is likely related, and although it is less problematic (since the standard formats don't put spaces here), it is still an odd inconsistency:

var style = NumberStyles.AllowLeadingWhite |
    NumberStyles.AllowTrailingWhite |
    NumberStyles.AllowParenthesis;

int.Parse("(123   )", style) // Ok with AllowTrailingWhite
int.Parse("(   123)", style) // Always throws, even with AllowLeadingWhite 

Expected behavior

The first issue with the sign should be fixed so that AllowLeadingWhite allows whitespace to lead the number after the sign.

The second issue I'm a bit agnostic about, but I thought I should mention it. It's probably too late to make it stricter and not allow spaces on either side, so either loosen the behavior so whitespace can lead the number after the opening parenthesis (which may just happen naturally as part of the fix to the first issue), or leave it as-is.

Actual behavior

See above.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8

Other information

No response

Activity

  1. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on May 4, 2024
  2. changed the title [-]Negative numeric parsing inconsistencies / can't parse standard OS format option[/-] [+]Negative numeric parsing inconsistencies / can't parse standard Windows format option[/+] on May 4, 2024
  3. added and removed
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on May 4, 2024
  4. dotnet-policy-service commented on May 4, 2024

    @dotnet-policy-service
    Contributor

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

  5. danmoseley commented on May 4, 2024

    @danmoseley
    Contributor

    Not sure whether number parsing should be labeled numerics

  6. added this to the Future milestone on Jul 20, 2024
  7. dotnet-policy-service commented on Jul 20, 2024

    @dotnet-policy-service
    Contributor

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

  8. dotnet-policy-service commented on Jul 20, 2024

    @dotnet-policy-service
    Contributor

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

  9. tarekgh commented on Jul 20, 2024

    @tarekgh
    Member

    @jeffhandley I moved the area back to numeric. It is parsing issue with numeric which need to consider the globalization settings. I can help with any globalization guidance as needed.

  10. jeffhandley commented on Jul 20, 2024

    @jeffhandley
    Member

    OK, thanks for the course correction there, @tarekgh.

  11. added a commit that references this issue on Sep 25, 2026
    7bdcc5f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions