problem with converting to Double
-
Dear all, I have a little problem converting a string that contains a ',' into a double.
Double
needs a value devided by a dot ( or so I've read ) So , in my code , the following occurs :string lat = p[u].X.ToString().Replace(',', '.');
MessageBox.Show(lat);The output will be for example : 55.38978 ( which shows up perfect in the messagebox ) However, if i do the following :
MessageBox.Show(Convert.ToDouble(lat).ToString());
The output will be : 5538978 That doesn't make a whole lot of sense to me. So that dot is somehow bothering
Double
or am I not converting properly ? Cheers :) -
Dear all, I have a little problem converting a string that contains a ',' into a double.
Double
needs a value devided by a dot ( or so I've read ) So , in my code , the following occurs :string lat = p[u].X.ToString().Replace(',', '.');
MessageBox.Show(lat);The output will be for example : 55.38978 ( which shows up perfect in the messagebox ) However, if i do the following :
MessageBox.Show(Convert.ToDouble(lat).ToString());
The output will be : 5538978 That doesn't make a whole lot of sense to me. So that dot is somehow bothering
Double
or am I not converting properly ? Cheers :)Well the problem is that double works with a komma in stead of a dot. See it like this:
string ss = "55,5493"; string lat = ss.ToString().Replace(',', '.'); double d = Convert.ToDouble(ss); double dd = Convert.ToDouble(lat); MessageBox.Show("" + d); //output: 55,5493 MessageBox.Show("" + dd); //oubput: 555493
So you should change this one:
string lat = ss.ToString().Replace(',', '.');
into this one:
string lat = ss.ToString().Replace('.', ',');
Good luck
-
Dear all, I have a little problem converting a string that contains a ',' into a double.
Double
needs a value devided by a dot ( or so I've read ) So , in my code , the following occurs :string lat = p[u].X.ToString().Replace(',', '.');
MessageBox.Show(lat);The output will be for example : 55.38978 ( which shows up perfect in the messagebox ) However, if i do the following :
MessageBox.Show(Convert.ToDouble(lat).ToString());
The output will be : 5538978 That doesn't make a whole lot of sense to me. So that dot is somehow bothering
Double
or am I not converting properly ? Cheers :)Hi Rick, ToString() when applied to numbers will by default use the regional settings of your system, which define a "decimal point" (not necessarily a period) and a "thousands separator" (not necessarily a comma). Parse() and TryParse() will observe the "decimal point" and ignore the "thousands separator". If you have a number formatted one way (say West-European way, with decimal comma, and thousands period) and parsed another way (say US way), you will get wrong results, and even an Exception when more than one thousands separator is present. The solution is NOT to replace periods by comma and vice versa, the solution is to apply the appropriate settings, by using a FormatProvider or CultureInfo. The above applies not only to doubles, but also to floats and decimals. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|