开发者

int.Parse of "8" fails. int.Parse always requires CultureInfo.InvariantCulture?

开发者 https://www.devze.com 2022-12-28 20:22 出处:网络
We develop an established software which works fine on all known computers except one. The problem is to parse strings that begin with \"8\". It seems like \"8\" in the beginning of a string is a rese

We develop an established software which works fine on all known computers except one. The problem is to parse strings that begin with "8". It seems like "8" in the beginning of a string is a reserved character.

Parsing:
int.Parse("8") -> Exception message: Input string was not in a correct format. 
int.Parse("80") -> 0
int.Parse("88") -> 8
int.Parse("8100") -> 100

CurrentCulture: sv-SE 
CurrentUICulture: en-US

The problem is solved using int.Parse("8", CultureInfo.InvariantCulture). However, it would be nice to know the source of the problem.

Question: Why do we get this behaviour of "8" if we don't specify invariant culture?


Additional information:

I did send a small program to my client achieve the result above:

    private int ParseInt(string s)
    {
        int parsedInt = -1000;
        try
        {
            parsedInt = int.Parse(s);

            textBoxMessage.Text = "Success: " + parsedInt;

        }
        catch (Exception ex)
        {
            textBoxMessage.Text =
                string.Format("Error parsing string: '{0}'", s) + Environment.NewLine +
                "Exception message: " + ex.Message;
        }

        textBoxMessage.Text += Environment开发者_StackOverflow.NewLine + Environment.NewLine +
            "CurrentCulture: " + Thread.CurrentThread.CurrentCulture.Name + "\r\n" +
            "CurrentUICulture: " + Thread.CurrentThread.CurrentUICulture.Name + "\r\n";
        return parsedInt;
    }

Update

I stumbled across this link, a bug in the microsoft connect database:

https://connect.microsoft.com/VisualStudio/feedback/details/253265/int32-parse-fails-to-convert-the-string-0-zero-on-some-systems

It seems like there's an issue with similiar symptoms, but no real root cause. If anyone could elaborate on this I would be grateful!


For the sv-SE culture 8 represents CurrencyNegativePattern and that's why you're getting the error you describe.

You can check this by running the following example:

var ci = new CultureInfo("sv-SE");

var nfi = (NumberFormatInfo)ci.GetFormat(typeof(NumberFormatInfo));

Console.WriteLine(nfi.CurrencyNegativePattern);
Console.WriteLine(nfi.CurrencyPositivePattern);

This will output:

// 8
// 3

You can explicitly say that you are parsing an integer and not a currency by using the Parse overload that accepts a NumberStyles enumeration.

Int32.Parse("8", NumberStyles.Integer, new CultureInfo("sv-SE"));

This time since you are specifying that you're parsing an integer no error will occur.


However, IIRC the Int32.Parse should interpret the input as an integer by default, so why you're getting to the error with that sample code is beyond me.


Update:

From the information you recently added it seems that you should make sure that the problem is not external. This is, if the user for example changed the positive sign setting of the windows locale to 8 it would be normal and make perfect sense for you to get the error you are obtaining. It would be just like setting the + as the positive sign and then trying to parse it:

var ci = new CultureInfo("sv-SE");
var nfi = (NumberFormatInfo)ci.GetFormat(typeof(NumberFormatInfo));

nfi.PositiveSign = "+";

Int32.Parse("+", nfi); // This will throw

Ask the user for it's locale registry settings like indicated in the Connect issue and check that they are what you would expect.

Side note: Welcome to SO and by the way next time you need to add further information to your question you should edit it instead of providing it in an answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消