Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing numbers with double.TryParse strange behaviour

Tags:

c#

parsing

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.

?

like image 818
mare Avatar asked Jun 28 '11 11:06

mare


2 Answers

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);
like image 114
Daniel Hilgarth Avatar answered Oct 05 '22 18:10

Daniel Hilgarth


This worked for me

double.TryParse("1.035,00",
NumberStyles.Any,
GlobalSettings.Instance.DefaultCulture, out price);
like image 31
V4Vendetta Avatar answered Oct 05 '22 19:10

V4Vendetta