Parse a string to a float (or double) in C#
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { var s = "3.14159265358979323846"; float f = float.Parse(s); Console.WriteLine(f); // 3.1415927 } } |
Similarly, you can use the Double.Parse() method to get a double from a string, which can hold several digits of precision.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { var s = "3.14159265358979323846"; double d = double.Parse(s); Console.WriteLine(d); // 3.141592653589793 } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Globalization; public class Example { public static void Main() { var s = "3.14159265358979323846"; double f = double.Parse(s, CultureInfo.InvariantCulture.NumberFormat); Console.WriteLine(f); // 3.141592653589793 } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { var s = "3.14159265358979323846"; double d = Convert.ToDouble(s); Console.WriteLine(d); // 3.141592653589793 } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Globalization; public class Example { public static void Main() { var s = "3.14159265358979323846"; double d = Convert.ToDouble(s, CultureInfo.InvariantCulture.NumberFormat); Console.WriteLine(d); // 3.141592653589793 } } |
To get the corresponding float value (or integral value) from the returned Double value, use casting. The following example demonstrates its usage.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { var s = "3.14159265358979323846"; float f = (float) Convert.ToDouble(s); Console.WriteLine(f); // 3.1415927 } } |
That’s all about parsing a string to a float (or double) in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)