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#
// C# program to Take input // of a float value using // Single.Parse() Method using System; using System.Text; public class GFG{ // Main Method static void Main(string[] args) { //declaring a float variables float value = 0.0f; // use of Single.Parse() Method value = Single.Parse(Console.ReadLine()); //printing the value 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#
// C# program to Take input // of a float value using // float.Parse() Method using System; using System.Text; public class GFG{ // Main Method static void Main(string[] args) { // declaring a float variables float value = 0.0f; // use of float.Parse() Method value = float.Parse(Console.ReadLine()); // printing the value 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#
// C# program to Take input // of a float value using // Convert.ToSingle() method using System; using System.Text; public class GFG{ // Main Method static void Main(string[] args) { // declaring a float variable float value = 0.0f; // use of Convert.ToSingle() Method value = Convert.ToSingle(Console.ReadLine()); // printing the value Console.WriteLine("Value = {0}", value); } } |
Console Input:
12.34
Output:
Value = 12.34
Recommended Posts:
- LINQ | Partition Operator | Take
- Different ways to create an Object in C#
- Different ways to sort an array in descending order in C#
- Different ways to make Method Parameter Optional in C#
- Different ways to convert String to Integer in C#
- Different Ways to Convert Double to Integer in C#
- Different Ways to Convert Char Array to String in C#
- Program to Input Weekday Number and Print the Weekday in C#
- Adding Text with a Float number Using String.Format() Method in C#
- float keyword in C#
- Different Methods to Read a Character in C#
- Different Types of HTML Helpers in ASP.NET MVC
- C# Program for Count ways to reach the n\'th stair
- C# | How to get the Standard Input Stream through Console
- C# | Check if Input is Redirected on the Console or not
- C# | How to change the Input Encoding Scheme of the Console
- How to Mask input in MaskedTextBox in C#?
- C# | Add key and value into StringDictionary
- C# | Add the specified key and value into the ListDictionary
- C# | Adding the specified key and value into HybridDictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

