Convert.ToDouble and Culture

  • Thread starter Thread starter John Bowman
  • Start date Start date
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,
 
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.
 
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
 
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
 
Back
Top