In this blog post, we are going to learn how we can convert a string to float in C#.

If you have a string that has a floating point value or a double stored in it, and you want to convert the string to a float so that you can use it in your application.

Fortunately, converting a string to a float is an easy thing to do and we will see the best way to make this conversion.

 

 

 

 

Convert String To Float Using Float.Parse Method

If we have a string that is holding a floating point value, and we want to convert that to a float type, we can make use of the float.Parse() method and convert the string to float in C#.

Let's see this in an example.

 

Example:

using System.Globalization; namespace ConsoleApp { public class Program { public static void Main() { string strFloatValue = "119.8762"; float floatValue = float.Parse(strFloatValue, CultureInfo.InvariantCulture.NumberFormat); Console.WriteLine(floatValue); } } }

 

Result:

// String To Float In C# Using float.Parse() 119.8762

 

In the above code example, we have a string which holds the floating point value of 119.8762.

We wanted to convert this string value to a float in C# and we made use of the .Parse() method on a float type.

This takes a string type.

As the second parameter, we also gave the IFormatProvider and we gave the value CultureInfo.InvariantCulture.NumberFormat that returns the culturally appropriate format of displaying numbers, currency and percentage.