Hi,
I want to convert a string to double. I use the function:
"System.Convert.ToDouble".
The problem is that if the string contains the character "." the program
aborts.
What might be the problem ?
Yoav.
Jon Skeet [C# MVP] - 26 Sep 2007 11:59 GMT
> I want to convert a string to double. I use the function:
> "System.Convert.ToDouble".
> The problem is that if the string contains the character "." the program
> aborts.
>
> What might be the problem ?
Chances are you're using a culture which doesn't use "." as the
decimal separator.
Try specifying CultureInfo.InvariantCulture as the culture in the
Convert.ToDouble call.
Jon
Göran Andersson - 26 Sep 2007 12:00 GMT
> Hi,
> I want to convert a string to double. I use the function:
[quoted text clipped - 5 lines]
>
> Yoav.
The ToDouble method is using a NumberFormat object that specifies
different characters for decimal separator and thousands separator than
what you have in the string.
The method uses the NumberFormat of the CultureInfo.CurrentCulture object.
You can use the Double.Parse method instead, so that you can specify the
FormatInfo or CultureInfo object directly. For a string that uses a
period as decimal separator, you can use the InvariantCulture object:
string s = "2.1415926536";
double d = double.Parse(s, CultureInfo.InvariantCulture);

Signature
Göran Andersson
_____
http://www.guffa.com
Yoavo - 30 Sep 2007 06:58 GMT
I found what was the problem.
I was using "," as decimal point instad of "."
Isn't this function should work OK if I am using "," as decimal point ??
> Hi,
> I want to convert a string to double. I use the function:
[quoted text clipped - 5 lines]
>
> Yoav.
Göran Andersson - 30 Sep 2007 11:54 GMT
> I found what was the problem.
> I was using "," as decimal point instad of "."
> Isn't this function should work OK if I am using "," as decimal point ??
It works just fine if you specify a FormatInfo or CultureInfo that
correctly describes how you write the number. Example:
string s = "3,1415926536";
CultureInfo swedish = new CultureInfo(1053);
double d = double.Parse(s, swedish);

Signature
Göran Andersson
_____
http://www.guffa.com