C# float Keyword with Example

In this tutorial, we will learn about the float keyword, its usage, memory size with example in C#. By IncludeHelp Last updated : April 04, 2023

C# float keyword

The float keyword is used to declare a variable that can store a floating point value between the range of ±1.5 x 10−45 to ±3.4 x 1038. The float is an alias of System.Single.

It occupies 4 bytes (32 bits) space in the memory.

Note: To represent a float value, we use a suffix f or F with the value.

Syntax

float variable_name = value;

Example of float keyword in C#

Here, we are declaring a float variable num, initializing it with the value 12345.6789f and printing its value, type and size of a float type variable.

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //variable declaration
      float num = 12345.6789f;

      //printing value
      Console.WriteLine("num: " + num);
      //printing type of variable
      Console.WriteLine("Type of num: " + num.GetType());
      //printing size
      Console.WriteLine("Size of a float variable: " + sizeof(float));
      //printing minimum & maximum value of float
      Console.WriteLine("Min value of float: " + float.MinValue);
      Console.WriteLine("Max value of float: " + float.MaxValue);

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

Output

num: 12345.68
Type of num: System.Single
Size of a float variable: 4
Min value of float: -3.402823E+38
Max value of float: 3.402823E+38



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.