Different Ways To Take Input And Print A Float Value In C#

In this guide, we will explore various techniques for receiving input and displaying a floating-point number in C# using diverse approaches.

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 method is employed to transform a string value into a float data type value. This procedure consists of the subsequent actions:

  • Single represents a class.
  • The Parse method belongs to it.
  • Syntax:

The syntax can be defined as follows:

Example

floatVa = Single.Parse(Console.ReadLine());

Example:

Let's consider an illustration of utilizing the float data type with the Single.Parse function in C#.

Filename: SingleParse.cs

Example

// 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:

Output

Value = 14.9

Explanation:

In this instance, the code initially defined a float variable set to 0.0, followed by soliciting user input. The input provided by the user is gathered as a string through the Console.ReadLine method and later converted to a float using Single.Parse. Ultimately, Console.WriteLine is employed to exhibit the converted float values on the console.

2. Parse Method

A predefined function in C# known as parseFloat within the Float Class yields a fresh float instance set to the value indicated by the given String. This functionality is achieved through the valueOf approach within the Float class.

  • float serves as an alternative name for the Single class.
  • The method utilized for this purpose is Parse.
  • Syntax:

It has the following syntax:

Example

floatVal = float.Parse(Console.ReadLine());

Example:

Let's consider an instance where we utilize a float to receive a decimal value through the float.Parse Method in C#.

Filename: FloatParse.cs

Example

// 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:

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 Content:

Using the

  • WriteLine method with the parameters ("Value = 0", val) will showcase the value of val in a formatted manner on the console output.
  • 3. ToSingle Method

This approach is applicable for transforming the numerical value into its equivalent single-precision floating-point number by utilizing the designated culture formatting details.

Syntax:

It has the following syntax:

Example

public static float ToSingle (string val, IFormatProvider provider);

Parameters:

The variable 'val' should be a string that holds the integer value for conversion.

A provider is an entity that offers formatting details tailored to various cultural settings.

If the value is null, this function will yield an integer equivalent to the number in the value or 0, in single-precision floating-point format.

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 following code examples demonstrate the utilization of the Convert.ToSingle(String, IFormatProvider) method:

Filename: ConvertToSingle.cs

Example

//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:

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.

Input Required

This code uses input(). Please provide values below: