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
Different Ways to Take Input and Print a Float Value in C#
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