In this article, we will discuss the different ways to take input and print a float value in C# with different methods.
We are aware that Console.ReadLine function can be used for reading the string to the output device. If the value is not of the string type by default, it is changed to the float type. There are several techniques for converting a given input to a float value. The below are the approaches which can be used are:
- Parse Method
- Parse Method
- ToSingle Method
1. Parse Method
The Single.Parse function is used for converting a string value to a float datatype value. This approach includes the following steps:
- Single is a class.
- Its method is Parse.
Syntax:
The syntax can be defined as follows:
floatVa = Single.Parse(Console.ReadLine());
Example:
Let us take an example of the float value using the Single.Parse method in C#.
Filename: SingleParse.cs
// C# implementation to use the Single.Parse() method
//to accept input of a float value
using System;
using System.Text;
public class SiingleParse{
// Main Method
static void Main(string[] args)
{
//the float variable declaration
float val = 0.0f;
// the Single.Parse() method is used
val= Single.Parse(Console.ReadLine());
//display of the value
Console.WriteLine("Value = {0}", val);
}
}
Input:
Output:
Value = 14.9
Explanation:
In this example, the code first declared the float variable, initialized to 0.0, and the user is then prompted for input. The user input is collected as a string by the Console.ReadLine function and is subsequently transformed into a float by Single.Parse. Finally, Console.WriteLine is used to display the parsed float values on the console.
2. Parse Method
A built-in C# function called parseFloat in the Float Class returns a new float that is initialized to its value, which is represented by the provided String. It is accomplished by the valueOf method of the Float class.
- float is the Single class's alias.
- The technique for it is Parse.
Syntax:
It has the following syntax:
floatVal = float.Parse(Console.ReadLine());
Example:
Let us take an example of using float to input a floating-point value using the float.Parse Method in C#.
Filename: FloatParse.cs
// C# implementation to use the float.Parse() method
//to accept input of a float value
using System;
using System.Text;
public class FloatParse{
// Main Method
static void Main(string[] args)
{
//the float variable declaration
float val = 0.0f;
// the Single.Parse() method is used
val= float.Parse(Console.ReadLine());
//display of the value
Console.WriteLine("Value = {0}", val);
}
}
Input:
Output:
Value = 45.9
Explanation:
Variable Declaration:
- float val = 0.0f; defines and creates a float variable val with 0.0f. Float input from the user.
- val = float in Parse.Parse(Console.ReadLine); it is used for reading the input from the console.
- Parse tries to convert the given text to a float value.
Displaying the Value:
- WriteLine("Value = 0", val); returns the value of val formatted inside the character string to the console.
3. ToSingle Method
This method can be used for converting the number to it's corresponding single-precision floating-point number using the provided specific culture structuring information.
Syntax:
It has the following syntax:
public static float ToSingle (string val, IFormatProvider provider);
Parameters:
val: A string containing the integer to be converted.
provider: It is an object that provides formatting information for different cultures.
Return Value: If val is null, this function returns a single-precision floating-point numbers integer that is comparable to the number in value or 0 (zero).
Exceptions:
- If the value does not include an optional sign it is followed by a series of digits (0 through 9).
- If the value reflects a number that is smaller than MinValue or larger than MaxValue , an OverFlowException is thrown.
Example:
The code samples below explain how to use Convert.ToSingle(String, IFormatProvider) method:
Filename: ConvertToSingle.cs
//Program to take floating point input using the Convert.ToString() method
using System;
using System.Text;
public class ConvertToSingle{
// Main Method
static void Main(string[] args)
{
// A float variable declaration
float val = 0.0f;
// use of Convert.ToSingle() Method
val = Convert.ToSingle(Console.ReadLine());
// display of the result floating value
Console.WriteLine("Value = {0}", val);
}
}
Input:
Output:
Value = 32.4
Explanation:
- float val = 0.0f; defines a float variable called val and sets its default value to 0.0f.
- val = Convert.ToSingle(Console.ReadLine); reads a line of text through the console provided by the user. Convert.ToSingle tries to convert the string returned by Console.ReadLine into a single-precision floating-point numbers value (float).
- WriteLine("Value = 0", val); returns the value of val formatted inside the character string to the console.