• Register
3,890 points
11 5 2

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.

Overloaded forms:

The two types of overloaded forms are;

  • ToDouble(String);
  • ToDouble(String, IFormatProvider);

ToDouble(String):

Syntax:

Public static double ToDouble(string strValue);

Example:

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.

ToDouble(String, IFormatProvider):

Syntax:

public static double ToDouble(string strValue, IFormatProvider provider);

Example:

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.

3,890 points
11 5 2