Lesson 2.11

Float data type C# - programming example


Objective

What is float data type in c#?
How to declare and initialize
Programming examples
Conversion and Casting

What is float Data Types in C#?

"Float is 32-bit floating point data type which is derived from System.Single Class. It stores value between -3.4x1038 to +3.4x1038. It is used for storing pointing or decimal type numeric value."

TypeApproximate RangePrecision.Net Framework type
Float-3.4x1038to +3.4x10387 DigitsSystem.Single

Declaration and Initialization

You can declare float data type as follow:

		  
  float num1=35; //or 
  float num1=35.23; //or
  float num1=35F; //you can suffix F in float value.

		  

Important Notes: A floating-point expression can contain the following sets of values:

1. Positive and negative zero
2. Positive and negative infinity
3. Not-a-Number value (NaN)
4. The finite set of nonzero values

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace float_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            float f1, f2, result;            
            f1 = 23.52F;
            f2 = 89.932F;
            result = f1 + f2; //Implicit Conversion
            Console.WriteLine(result);

            int res = (int)(f1 + f2); //Explicit Conversion
            Console.WriteLine(res);
            

            Console.ReadLine();
        }
    }
}


		  

Output

113.452
113
_

Conversion

1. Implicit Conversion - No need to convert when left hand side data type 
  is:   double
  
  Example:
  
  float a=5, b=6;
  double result=a+b;

2. Explicit Conversion - You need to convert when left hand side data type is: byte, sbyte, short, ushort, int, uint, long, decimal and char Example: float a=5, b=6; int result=(int)(a+b); Note: When converted float value into other integral type, the point precision gone.

Passing float value as parameter

When calling a function which requires float value in parameter you must cast value into respective datatype to avoid compilation error..

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace float_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            float var1=342.52;
            ShowNumber((int)(var1)); // Correct(√)

            ShowNumber(var1); // Incorrect(×)
        }

        public static void ShowNumber(int num1)
        {
            Console.WriteLine(num1);
            Console.ReadLine();
        }
    }
}
		  

Output

342
_

It is best habbit to cast or convert data into respective datatype everywhere. It will save you from unnecessary runtime errors and makes your code more robust.

Explanation

In the above example we have created a function called ShowNumber(int num1) which requires an integer type value as parameter and then display the number.

We have called this function in our main class in which we casted float value into int type before passing.
 float var1=342;
 ShowNumber((int)(var1)); // Correct(√)

 If we don’t cast it will raise compilation error as follow :
 ShowNumber(var1); // Incorrect(×)

 Compilatation Error :Error 1: The best overloaded method match for 'float_data_type.Program.ShowNumber(int)' has some invalid arguments
 Error 2: cannot convert from 'float' to 'int'
  

Summary

float is 32-bit integer type data type which stores value between -3.4x1038to +3.4x1038. It is mostly used for storing very large decimal value. In the next chapter we will discuss on double data type.

BackNext