Convert.ToDouble and Culture

J

John Bowman

Hello,

I've got a string retrieved from an XML data file, that in reality
represents a double. So, an example in English would be the string
"1.23456789", but in say German or French it would be "1,23456789" (but the
numbers are in reality very large). I need to convert this into a double to
pass on to a 3rd party trend chart object that requires a double. My
question is that if I use the Convert.ToDouble() method, does it handle the
culture correctly? Does it return the value 1.23456789 in English but
1,23456789 in German/French correctly?

TIA,
 
J

Jon Skeet [C# MVP]

John Bowman said:
I've got a string retrieved from an XML data file, that in reality
represents a double. So, an example in English would be the string
"1.23456789", but in say German or French it would be "1,23456789" (but the
numbers are in reality very large). I need to convert this into a double to
pass on to a 3rd party trend chart object that requires a double. My
question is that if I use the Convert.ToDouble() method, does it handle the
culture correctly? Does it return the value 1.23456789 in English but
1,23456789 in German/French correctly?

Yes, but to make it clear that you particularly want this behaviour,
I'd specify the relevant CultureInfo as the second parameter to
Convert.ToDouble.
 
J

Jochen Kalmbach

John said:
Hello,

I've got a string retrieved from an XML data file, that in reality
represents a double. So, an example in English would be the string
"1.23456789", but in say German or French it would be "1,23456789"
(but the numbers are in reality very large). I need to convert this
into a double to pass on to a 3rd party trend chart object that
requires a double. My question is that if I use the Convert.ToDouble()
method, does it handle the culture correctly? Does it return the value
1.23456789 in English but 1,23456789 in German/French correctly?

The Convert.ToDouble(string) uses the actual culure to convert the
string...
So for example if the current locale is english it will correctly convert
"1.234" but will fail to convert "1,234" !!!

My solution to this problem is:

1. Replace in the string all "," with "."
2. Use double.Parse(string, Culure.InvariantCulture)


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 
J

John Bowman

Thanks, er, uh, ... danke / merci <g>

John

Jochen Kalmbach said:
The Convert.ToDouble(string) uses the actual culure to convert the
string...
So for example if the current locale is english it will correctly convert
"1.234" but will fail to convert "1,234" !!!

My solution to this problem is:

1. Replace in the string all "," with "."
2. Use double.Parse(string, Culure.InvariantCulture)


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top