mare
mare

Reputation: 13083

Parsing numbers with double.TryParse strange behaviour

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.

?

Upvotes: 7

Views: 1887

Answers (2)

V4Vendetta
V4Vendetta

Reputation: 38230

This worked for me

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

Upvotes: 2

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174467

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);

Upvotes: 6

Related Questions