This article illustrates the different techniques to parse a string to a float (or double) in C#.

1. Using Double.Parse() method

You can convert the string representation of a number to its floating-point number equivalent using the Parse() method. For example, the following code parses a string to a float:

Download  Run Code

 
Similarly, you can use the Double.Parse() method to get a double from a string, which can hold several digits of precision.

Download  Run Code

 
The following solution uses the Parse(String, IFormatProvider) method to convert the string representation of a number in a specified culture-specific format to a Double value. Note that you need to add the System.Globalization namespace to access the CultureInfo object.

Download  Run Code

2. Using Convert.ToDouble() method

Alternatively, you can use the Convert.ToDouble() method to convert the specified value to the corresponding floating-point number with double-precision. The following code example shows a typical invocation for this method:

Download  Run Code

 
The Convert.ToDouble(String, IFormatProvider) method is overloaded to convert the string in a specified culture-specific format to a Double value. This is demonstrated below:

Download  Run Code

 
To get the corresponding float value (or integral value) from the returned Double value, use casting. The following example demonstrates its usage.

Download  Run Code

That’s all about parsing a string to a float (or double) in C#.