Home » C#.Net

Input and print a float value in C#

C# program to input and print a float value: Here, we are going to learn how to take input of a float value and print using C# program?
Submitted by IncludeHelp, on February 26, 2019

The task is to take an input of a float value and print it on the console in C#.

As, we have discussed in earlier programs that to read any value, we use Console.ReadLine() method and if the desired value is not in the string format, we need to convert it into the specific type.

There are some of the common and popular ways to convert a value to the float,

  1. float.Parse() method – Here, float is an alias of Single class and Parse() is its method – it converts given string value to the float value.
    Syntax:
        float_value = float.Parse(Console.ReadLine());
    
  2. Single.Parse() methodSingle is a class and Parse() its method – it converts a string value to the float value.
    Syntax:
        float_value = Single.Parse(Console.ReadLine());
    
  3. Convert.ToSingle() method – Here, ToSingle() is a method of Convert class – it converts given object into the float value.
    Syntax:
        float_value = Convert.ToSingle(Console.ReadLine());
    

Example:

In the below example, we have a variable value of "float" type, we are taking input from the user three times, and converting the value using all of the above-mentioned methods.

Note: float.Parse() and Single.Parse() both are similar, because float is an alias of Single class.

C# code to read a float value and print it on the console

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaring a float variables and assigning it with 
            //a default value
            float value = 0.0f;

            //prompt message to take input 
            //using float.Parse()
            Console.Write("Enter a float value: ");
            value = float.Parse(Console.ReadLine());
            //printing the value
            Console.WriteLine("value = {0}", value);

            //prompt message to take input 
            //using Single.Parse()
            Console.Write("Enter a float value: ");
            //resetting the value 
            value = 0.0f;
            value = Single.Parse(Console.ReadLine());
            //printing the value
            Console.WriteLine("value = {0}", value);

            //prompt message to take input 
            //using Convert.ToSingle()
            Console.Write("Enter a float value: ");
            //resetting the value 
            value = 0.0f;
            value = Convert.ToSingle(Console.ReadLine());
            //printing the value
            Console.WriteLine("value = {0}", value);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

Enter a float value: 123.456
value = 123.456
Enter a float value: 456.789
value = 456.789
Enter a float value: 012.45
value = 12.45





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.