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

1

11

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.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!

Kisser answered 27/4, 2010 at 9:26 Comment(0)
M
21

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.

Mattern answered 27/4, 2010 at 9:29 Comment(2)
Thanks for the side note, i took care of it :) I've checked earlier the values in the registry for his locale settings and the only 8 out there is the CurrencyNegativePattern which doesn't help me reproduce the error on my machine. You're definately of the right track here João. However, this issue is a hard one and I'm not sure Microsoft knows the reason of the issue either. You're getting a big UP for sure, but the behaviour is still a mystery. That's why i'm not accepting you're answer as a solution even though I'm grateful for your effort!Kisser
The beginning of this answer seems wrong to me. The values in NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern are used only as keys - to identify the order of negative sign (-), currency sign, and the number itself that will be used for formatting/parsing. For example, '8' as value of CurrencyNegativePattern means that the pattern should be '-n $'.Osborne

© 2022 - 2024 — McMap. All rights reserved.