21

I have a double number as string. The number is

202.667,40

Which is 202667.4

How can I parse this string to get the value like: Double.Parse("202.667,40",?what here), or any other method to get the value would be great. Thanks

Ryan
  • 5,456
  • 25
  • 71
  • 129
  • 6
    Use the CultureInfo of a culture that uses a comma as a decimal point. Germany for example. Preferably matching whatever country the string came from. – Hans Passant Feb 24 '11 at 20:07

7 Answers7

35

First, you need to know which culture this number is from, then:

CultureInfo culture = new CultureInfo("de"); // I'm assuming german here.
double number = Double.Parse("202.667,40", culture);

If you want to parse using the current thread culture, which by default is the one set for the current user:

double number = Double.Parse("202.667,40", CultureInfo.CurrentCulture);
Coincoin
  • 27,880
  • 7
  • 55
  • 76
  • 4
    Using "de" causes a NotSupportedException to get thrown for me. "Culture 'de' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture." Perhaps you wanted "de-DE"? – Jeffrey LeCours Apr 14 '14 at 16:10
11

I think i have found a solution which does not require a culture. Using a NumberFormatInfo you can force a format, no matter the culture:

// This is invariant
NumberFormatInfo format = new NumberFormatInfo();
// Set the 'splitter' for thousands
format.NumberGroupSeparator = ".";
// Set the decimal seperator
format.NumberDecimalSeparator = ",";

Then later:

System.Diagnostics.Debug.WriteLine(double.Parse("202.667,40", format)));

Outputs:
202667,4

Of course, this output (inner toString()) might differ per Culture(!)
Note that changing the input to "202,667.40" will result in a parse error, so the format should match your expected input.

Hope this helps someone..

patman77
  • 119
  • 2
3

Instead of having to specify a locale in all parses, I prefer to set an application wide locale, although if string formats are not consistent across the app, this might not work.

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-PT");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("pt-PT");

Defining this at the begining of your application will make all double parses expect a comma as the decimal delimiter.

Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
2
var val=double.Parse( yourValue, CultureInfo.InvariantCulture);

http://www.erikschierboom.com/2014/09/01/numbers-and-culture/

Baouche Iqbal
  • 105
  • 1
  • 13
2

You could use Double.Parse(your_number, CultureInfo.CurrentCulture) and set CurrentCulture accordingly with Thread.CurrentThread.CurrentCulture.

Example:

Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");

then later

Double.Parse(your_number, CultureInfo.CurrentCulture);

Note that if you explicitly assign the culture to the CurrentThread, it only applies to that thread.

Holstebroe
  • 4,993
  • 4
  • 29
  • 45
2

For more flexibility you can set NumberDecimalSeparator

string number = "202.667,40";
double.Parse(number.Replace(".", ""), new CultureInfo(CultureInfo.CurrentCulture.Name) {NumberFormat = new NumberFormatInfo() {NumberDecimalSeparator = ","}});
StuiterSlurf
  • 2,464
  • 4
  • 34
  • 55
Yuriy Naydenov
  • 1,875
  • 1
  • 13
  • 31
1
Double.Parse("202.667,40", new System.Globalization.CultureInfo("de-DE"));

Instead of de-DE use whatever culture the string is in.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126