3 methods in C# to convert string to float

C#

Sometimes we have to convert the float value as string type to float. We are going to use some built-in methods in C# to convert string to float.

3 Methods in C# to convert string to float


  • Using Convert.ToDouble()
  • Using float.parse()
  • Using single.Parse()

1. Convert.ToDouble() method to Convert string to float C#


The Convert.ToDouble() method converts a string representation of a number to a floating-point number to two precision.

Syntax

Convert.ToDouble(string)

Parameters

string: A string value that to convert into float.

1.2 C# Program to Convert string to float


In this C# Program example, we are asking the user to enter some floating value and reading value using Console.ReadLine() method always takes the input into the string format.

We are converting the string value to float using Convert.ToDouble() method and print the result on the console.

using System.Globalization
public class conversion
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Please Enter value :");
            string str_val = Console.ReadLine();
            double result = Convert.ToDouble(str_val);
            Console.WriteLine("string to float:");
            Console.WriteLine(result);
            Console.ReadLine();

        }
    }

Output:

Please Enter float value :
76.89
string to float:
76.89

2. float.Parse() method in C# to Convert string to float


The float.Parse() method allows us to convert the string representation of a number to a single-precision float point number.

Syntax

float.Parse(string)

Parameter:

string: The string value we have to convert to float

2.1 C# Program to Convert string to float


In this C# program, we are asking users to enter a value and reading this value using Console.ReadLine() method in string format.

We will convert user input string value to float using float.Parse() method.

Finally printing the result using the Console.WriteLine(result)

using System
using System.Globalization
public class conversion
    {
        public static void Main(string[] args)
        {
            
            Console.WriteLine("Please Enter float value :");
            string str_val = Console.ReadLine();
            float result = float.Parse(str_val,CultureInfo.InvariantCulture.NumberFormat );
            Console.WriteLine("string to float:");
            Console.WriteLine(result);
            Console.ReadLine();

        }
    }

Output

Please Enter float value :
76.89
string to float:
76.89

3. Convert.ToSingle() in C# to Convert string to float


The Convert.ToSingle() method converts the string representation of a number to an equivalent single-precision floating-point number.

Syntax

ToSingle(string)

Parameter:

  • string: The string value we have to convert to float.

let us understand with an example below.

3.1 C# Program to Convert string to float


In this C# program, we are asking users to enter a value and reading this value using Console.ReadLine() method in string format.

We will convert user input string value to float using Convert.ToSingle(str_val) method.

Finally printing the result using the Console.WriteLine(result)

using System
public class conversion
    {
        public static void Main(string[] args)
        {
            

            Console.WriteLine("Please Enter float value :");
            string str_val = Console.ReadLine();
            float float_val = Convert.ToSingle(str_val);            
            Console.WriteLine("string to float:");
            Console.WriteLine(float_val);
            Console.ReadLine();

        }
    }

Output

Please Enter float value :
76.89
string to float:
76.89

Summary

In this post, we have learned how to use 3 methods in C# to convert string to float

  • Convert.ToDouble() method to convert string to float C#
  • float.Parse() method in C# to Convert string to float
  • Convert.ToSingle() in C# to Convert string to float