5

I am trying to parse some string to a double value, using this parse method overload:

double.Parse("198.222213745118", CultureInfo.CurrentUICulture);

the CultureInfo.CurrentUICulture is fr-FR. but this is throwing an exception of type FormatException.

What can be the reason?

Filburt
  • 17,626
  • 12
  • 64
  • 115
user2717436
  • 775
  • 1
  • 10
  • 23

3 Answers3

5

French (i.e. fr-FR) use a comma to denote the start of the decimal part, not a period. They use a period to separate thousands.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Sure, but for this reason I sent my culture to the parse method. – user2717436 Sep 17 '13 at 10:39
  • @user2717436, the culture needs to be consistent with that in the UI control. – Bathsheba Sep 17 '13 at 10:40
  • Yes, preciselly, and the thousand separator part is only in written language, the same with Romanian culture. `double.Parse` will not stand for `.` it will throw an exception. – Eduard Dumitru Sep 17 '13 at 10:41
  • So how can I do it? I mean - this string is read from xml file, and I got it just like this (with "," separator). how can I parse it to double depends on my culture? – user2717436 Sep 17 '13 at 10:57
  • The XML file should use a specific format. Specify a culture that has a format consistent with the one used in the XML file. – Klas Lindbäck Feb 11 '16 at 09:33
0

I know this question is old but my answer might help someone else. So this is the answer:

double.Parse("198.222213745118", System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo);

instead of

double.Parse("198.222213745118", CultureInfo.CurrentUICulture);
NutCracker
  • 11,485
  • 4
  • 44
  • 68
-2

I suggest using the Double.TryParse rather than the .Parse as it is safer to use and makes sure you dont get any exception on parsing.

Here is a Code for you to use,

double answer = -1;
Double.TryParse("Value", out answer);

Now all you have to do is do a conditional statement that'll check if it indeed parsed' the string.

DevEstacion
  • 1,947
  • 1
  • 14
  • 28