views:

432

answers:

3

I have a source XML that uses a dot (".") as a decimal separator and I am parsing this on a system that uses a comma (",") as a decimal separator.

As a result, value of 0.7 gets parsed with Double.TryParse or Double.Parse as 7000000.

What are my options to parse correctly? One of them is to replace dots in source with commas with String.Replace('.', ',') but I don't think I like this very much.

+4  A: 

double.TryParsehas an overload taking an IFormatProvider. Use a coresponding CultureInfo, in your case CultureInfo.InvariantCulture can be used.

Femaref
A: 

This does the job:

    string test = "0.7";
    Assert.Equal(0.7, Double.Parse(test, NumberStyles.Float, CultureInfo.InvariantCulture));
asgerhallas
Why the downvote?
asgerhallas
+6  A: 

XML standard is explicit about the formatting of dates and numbers etc. This helps to ensure that the XML is platform independent and interoperable. Take a look at using XmlConvert for xml data.

double value = XmlConvert.ToDouble(stringValue);
Chris Taylor
This is a good recommendation as many people are unaware of the XmlConvert class. This is particularly important when working with DateTime values.
Josh Einstein
Yes, I was unaware of this one too. Thanks!
asgerhallas