Skip to content

int.Parse("-1") throwing FormatException due to changed NumberFormat NegativeSign in .NET 5 preview 7 #40922

@vholmer

Description

@vholmer

Description

In 5.0.100-preview.7.20366.6 it seems as if the following code throws a FormatException. However, if I pass CultureInfo.InvariantCulture to the parse function it seems to work.

From what I can gather NegativeSign for my NumberFormat was changed from "-" to "−", I'm not sure whether this is due to some change in preview 7. According to my region settings the negative sign is not "−" but actually "-" whereas NumberFormat says that it's "−". Is this really correct?

image

Here's the code:

using System;
using System.Globalization;

namespace IntParseNegative
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(CultureInfo.CurrentCulture);

            try
            {
                int.Parse("-1");
            }
            catch (FormatException fe)
            {
                Console.WriteLine(fe.Message);
            }

            int.Parse("-1", CultureInfo.InvariantCulture);
        }
    }
}

Configuration

The code is running 5.0.100-preview.7.20366.6 on Windows 10 Enterprise version 1909 on x64 architecture.

Regression

In 5.0.100-preview.3.20216.6 this code did not throw an exception.

Other information

Here's my CultureInfo.CurrentCulture:

{sv-SE}
    Calendar: {System.Globalization.GregorianCalendar}
    CompareInfo: {CompareInfo - sv-SE}
    CultureTypes: SpecificCultures | InstalledWin32Cultures
    DateTimeFormat: {System.Globalization.DateTimeFormatInfo}
    DisplayName: "svenska (Sverige)"
    EnglishName: "Swedish (Sweden)"
    IetfLanguageTag: "sv-SE"
    IsNeutralCulture: false
    IsReadOnly: true
    KeyboardLayoutId: 1053
    LCID: 1053
    Name: "sv-SE"
    NativeName: "svenska (Sverige)"
    NumberFormat: {System.Globalization.NumberFormatInfo}
    OptionalCalendars: {System.Globalization.Calendar[1]}
    Parent: {sv}
    TextInfo: {TextInfo - sv-SE}
    ThreeLetterISOLanguageName: "swe"
    ThreeLetterWindowsLanguageName: "SVE"
    TwoLetterISOLanguageName: "sv"
    UseUserOverride: true

Here's CultureInfo.CurrentCulture.NumberFormat:

{System.Globalization.NumberFormatInfo}
    CurrencyDecimalDigits: 2
    CurrencyDecimalSeparator: ","
    CurrencyGroupSeparator: " "
    CurrencyGroupSizes: {int[1]}
    CurrencyNegativePattern: 8
    CurrencyPositivePattern: 3
    CurrencySymbol: "kr"
    DigitSubstitution: None
    IsReadOnly: true
    NaNSymbol: "¤¤¤"
    NativeDigits: {string[10]}
    NegativeInfinitySymbol: "−∞"
    NegativeSign: "−"
    NumberDecimalDigits: 3
    NumberDecimalSeparator: ","
    NumberGroupSeparator: " "
    NumberGroupSizes: {int[1]}
    NumberNegativePattern: 1
    PerMilleSymbol: "‰"
    PercentDecimalDigits: 3
    PercentDecimalSeparator: ","
    PercentGroupSeparator: " "
    PercentGroupSizes: {int[1]}
    PercentNegativePattern: 0
    PercentPositivePattern: 0
    PercentSymbol: "%"
    PositiveInfinitySymbol: "∞"
    PositiveSign: "+"

Here's CultureInfo.InvariantCulture.NumberFormat for comparison:

{System.Globalization.NumberFormatInfo}
    CurrencyDecimalDigits: 2
    CurrencyDecimalSeparator: "."
    CurrencyGroupSeparator: ","
    CurrencyGroupSizes: {int[1]}
    CurrencyNegativePattern: 0
    CurrencyPositivePattern: 0
    CurrencySymbol: "¤"
    DigitSubstitution: None
    IsReadOnly: true
    NaNSymbol: "NaN"
    NativeDigits: {string[10]}
    NegativeInfinitySymbol: "-Infinity"
    NegativeSign: "-"
    NumberDecimalDigits: 2
    NumberDecimalSeparator: "."
    NumberGroupSeparator: ","
    NumberGroupSizes: {int[1]}
    NumberNegativePattern: 1
    PerMilleSymbol: "‰"
    PercentDecimalDigits: 2
    PercentDecimalSeparator: "."
    PercentGroupSeparator: ","
    PercentGroupSizes: {int[1]}
    PercentNegativePattern: 0
    PercentPositivePattern: 0
    PercentSymbol: "%"
    PositiveInfinitySymbol: "Infinity"
    PositiveSign: "+"

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.GlobalizationquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions