7

Why would double.TryParse() with these settings not parse

double.TryParse("1.035,00",
NumberStyles.AllowCurrencySymbol | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
GlobalSettings.Instance.DefaultCulture, out price);

where DefaultCulture is sl-SI (Slovenian), which has the dot . as digit grouping symbol and , as decimal point. The price remains 0 after the parse.

?

2
  • TryParse returns a bool that indicates if parsing the value succeeded or failed. For your example, TryParse returns false. So the cause for price being 0 is that the string couldn't be parsed. Commented Jun 28, 2011 at 11:23
  • 3
    I find it strange that some would vote close due to localization. If the problem was the culture then it would affect numerous cultures, all those that used comma as decimal separator, which is both the ISO standard and the representation used in most countries Commented Jun 28, 2011 at 11:28

2 Answers 2

6

You are missing NumberStyles.AllowThousands:

double.TryParse("1.035,00", NumberStyles.AllowCurrencySymbol | 
                            NumberStyles.AllowLeadingWhite | 
                            NumberStyles.AllowTrailingWhite |
                            NumberStyles.AllowDecimalPoint | 
                            NumberStyles.AllowLeadingSign | 
                            NumberStyles.AllowThousands,
                            GlobalSettings.Instance.DefaultCulture, out price);
Sign up to request clarification or add additional context in comments.

Comments

2

This worked for me

double.TryParse("1.035,00",
NumberStyles.Any,
GlobalSettings.Instance.DefaultCulture, out price);

2 Comments

would anyone else advise on using NumberStyles.Any in a sense, if there are any potential problems with it?
As per MSDN Indicates that all styles except AllowHexSpecifier are used. This is a composite number style.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.