convert a string in vb.net – convert.toDouble

Question:

I'm trying to convert a string to a double in vb.net:

 Dim result as double
 Dim ValueString as string = "-42.1942339"
 result = Convert.ToDouble(ValueString)
 Console.WriteLine(result)

I am getting the following value -421942339 What am I doing wrong?

Answer:

The problem is the culture being used to make the conversion. Most likely, your operating system is configured for a culture that does not consider the period (ie . ) as a decimal separator.

To make the conversion, in this case you cannot let the Convert.ToDouble method use the default culture of the operating system.

For this it is possible to pass a parameter to the Convert.ToDouble method indicating the culture:

Convert.ToDouble(ValueString, CultureInfo.InvariantCulture);
  • CultureInfo.InvariantCulture : is a culture that does not vary over time. The decimal point is always . .
Scroll to Top