In C#, almost all types of variables can be converted to any other data type. In the same way, we can convert string to double by using the ToDouble() Method inside the Convert class. There are many overloaded forms are available for this method and among those two types can be used to convert a string representation of a number to its equivalent floating-point number.
The two types of overloaded forms are;
- ToDouble(String, IFormatProvider);
Public static double ToDouble(string strValue);
Let's understand the working of the above method by the following example;
double doubleVal = Convert.ToDouble(“855.65”);
This statement will convert the 855.65 string by using the ToDouble method and the result will be stored in a variable of type Double.
public static double ToDouble(string strValue, IFormatProvider provider);
Let's understand the working of the above method with the help of the below example;
NumberFormatInfo provider = new NumberFormatInfo();
provider.NumberDecimalSeperator = “.”;
provider.NumberGroupSeperator = “,”;
double doubleVal = Convert.ToDouble(“855.65” , provider);
In this, we first create an object of IFormatProvider using the class NumberFormatInfo that implements IFormatProvider. After this, we set some important properties for this object like NumberGroupSeperator and NumberDecimalSeperator.