Double parse with culture format

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

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..

Tags:

C#

Int32