Skip to content

.Net 5.0 Int.Parse fails parsing with negative values. #40258

Closed

Description

@eriksimonic

.Net 5.0 Preview 7, fails on Int.Parse("-1") with
Message:
System.FormatException : Input string was not in a correct format.
Stack Trace:
Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
Int32.Parse(String s)
NetCore50prev7_Tests.NegativeIntegerFailsParse() line 15

public class NetCore50prev7_Tests
{
[Fact]
public void NegativeIntegerFailsParse()
{
int.Parse("-1");
}
}

Activity

  1. added
    untriaged
    on Aug 3, 2020
  2. Dotnet-GitSync-Bot commented on Aug 3, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    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.

  3. EgorBo commented on Aug 3, 2020

    @EgorBo
    Member

    @eriksimonic what is the current culture?

    Console.WriteLine(CultureInfo.CurrentCulture);
    

    and OS?

  4. eriksimonic commented on Aug 3, 2020

    @eriksimonic
    Author

    @EgorBo
    Windows 10 2004, build 19041.388
    Culture is sl-SI

    If I change the culture to CultureInfo.InvariantCulture, the parse works.

  5. EgorBo commented on Aug 3, 2020

    @EgorBo
    Member
    Console.WriteLine(string.Join(", ", Encoding.Unicode.GetBytes("-"))); // en-US negative sign
    Console.WriteLine(string.Join(", ", Encoding.Unicode.GetBytes("−"))); // sl-SI negative sign
    {45, 0}
    {18, 34}
    

    sl-SI locale uses a slightly different negative sign 🙂

    You can obtain it via new CultureInfo("sl-SI").NumberFormat.NegativeSign

  6. added
    question
    and removed
    untriaged
    on Aug 3, 2020
  7. eriksimonic commented on Aug 3, 2020

    @eriksimonic
    Author

    @EgorBo , this works correctly in .net core 5.0 or any other, It is this a "feature"

  8. EgorBo commented on Aug 3, 2020

    @EgorBo
    Member

    hm... indeed, 🤔

    Console.WriteLine(int.Parse("-1", new CultureInfo("sl-SI")));

    works for net4x and netcoreapp3.1 (windows)
    but doesn't work for net5 (windows and *nix) and netcoreapp3.1 (*nix, ICU)

  9. benaadams commented on Aug 3, 2020

    @benaadams
    Member

    Hasn't net5 moved to ICU on windows also?

  10. am11 commented on Aug 3, 2020

    @am11
    Member

    Yes. net5 switched to ICU library on Windows. Some cultures (e.g. en-US) work with multiple minus signs U+002D and U+221 with ICU. Apparently not the case with sl-SI. Interestingly, there was a related bug for Slovenian minus sign (U+2013), which was fixed last year in release 31: https://unicode-org.atlassian.net/browse/CLDR-10050.

  11. EgorBo commented on Aug 3, 2020

    @EgorBo
    Member

    @eriksimonic as a workaround you can define an env. variable DOTNET_SYSTEM_GLOBALIZATION_USENLS=true to rely on NLS instead of ICU on Windows.

  12. tarekgh commented on Aug 3, 2020

    @tarekgh
    Member

    Yes this is by design as we switched to ICU. sl-SI is not using - as the negative sign. If you are parsing with such culture you have to provide the correct negative sign. As mentioned earlier in the discussion, to workaround it, either use different culture e.g. Invariant, or enable NLS by the config switch System.Globalization.UseNls https://docs.microsoft.com/en-us/dotnet/standard/globalization-localization/globalization-icu.

  13. benaadams commented on Aug 3, 2020

    @benaadams
    Member

    You could also write a method that tries both

    int ParseInt(string value)
    {
        if (int.TryParse(value, out int result))
        {
            return result;
        }
    
        return int.Parse(value, CultureInfo.InvariantCulture);
    }
  14. eriksimonic commented on Aug 4, 2020

    @eriksimonic
    Author

    @benaadams , hard to do when third party library uses the method.
    @tarekgh why such a braking change?
    At least is <InvariantGlobalization>true</InvariantGlobalization> fisible option.
    Thanks.

  15. Clockwork-Muse commented on Aug 4, 2020

    @Clockwork-Muse
    Contributor

    @eriksimonic - Outside the move to ICU, this sort of thing is always possible; you can construct/use custom cultures on windows that would have the same effect.

    It might be handy to know what sort of third-party library you're using here; for instance, if you're doing data serialization/processing, you should likely be in the invariant culture anyways. Although I'd probably raise a bug with them if you can't override the culture/formatting options.

  16. tarekgh commented on Aug 4, 2020

    @tarekgh
    Member

    @eriksimonic, the answer of @Clockwork-Muse is correct. .NET 5.0 is by default using ICU and NLS. you have the option to switch back to NLS. but I wouldn't recommend that because NLS data can change too especially Windows working hard to converge with CLDR data which is shipped with ICU. As @Clockwork-Muse mentioned, if you want to guarantee the parsing, you should format and parse using Invariant culture.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Globalizationquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions