views:

227

answers:

2

In my app I parse a value from xml (string) to a double. The value in the xml happens to have the dot as a fraction seperator whereas the system takes the current system settings and can have a different separator (dev system takes the comma for example).

Is there a way to tell double.TryParse() the dot is the fraction separator?
Should I manually replace the dot with the system's fraction separator? If so, how do I get this?

A: 

Pass CultureInfo.InvariantCulture into double.TryParse:

double value;
bool success = double.TryParse(text, NumberStyles.Float,
                               CultureInfo.InvariantCulture,
                               out value);

(For genuinely standard XML formatting, Frederik's suggestion of using XmlConvert is the best idea though.)

Jon Skeet
+2  A: 

What you should do, in this situation, is use the XmlConvert class and its members to convert the value like it exists in the XML file to a regular variable. :)

Frederik Gheysels
I didn't know such a thing existed. And why exactly is this class better then double.trypars()? I understand it can come in handy when there are special characters involved. But there is no tryparse as far as I can see.
borisCallens