In C#, we know that Console.ReadLine() method is used to read string from the standard output device. Then this value is converted into the float type if it is not string type by default. There are different methods available to convert taken input to a float value. Following methods can be used for this purpose:
- Single.Parse() Method
- float.Parse() Method
- Convert.ToSingle() Method
Single.Parse() Method
The Single.Parse() method is used to convert given string value to the float value. This method is consist of the following:
- Single is a class.
- Parse() is its method.
Syntax:
float_value = Single.Parse(Console.ReadLine());
Example: Take input of a float value using Single.Parse() Method
C#
using System;
using System.Text;
public class GFG{
static void Main( string [] args)
{
float value = 0.0f;
value = Single.Parse(Console.ReadLine());
Console.WriteLine( "Value = {0}" , value);
}
}
|
Console Input:
12.34
Output:
Value = 12.34
float.Parse() Method
The float.Parse() method is used to convert given string value to the float value. This method is consist of the following:
- float is an alias of Single class.
- Parse() is its method.
Syntax:
float_value = float.Parse(Console.ReadLine());
Example: Take input of a float value using float.Parse() Method
C#
using System;
using System.Text;
public class GFG{
static void Main( string [] args)
{
float value = 0.0f;
value = float .Parse(Console.ReadLine());
Console.WriteLine( "Value = {0}" , value);
}
}
|
Console Input:
12.34
Output:
Value = 12.34
Convert.ToSingle() Method
The Convert.ToSingle() Method is used to convert given object to the float value. This method is consist of the following:
- Convertis a class.
- ToSingle() is its method.
Syntax:
float_value = Convert.ToSingle(Console.ReadLine());
Example: Take input of a float value using Convert.ToSingle() Method
C#
using System;
using System.Text;
public class GFG{
static void Main( string [] args)
{
float value = 0.0f;
value = Convert.ToSingle(Console.ReadLine());
Console.WriteLine( "Value = {0}" , value);
}
}
|
Console Input:
12.34
Output:
Value = 12.34
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!