C# decimal Keyword with Example

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

C# decimal Keyword

The decimal keyword used to declare a variable that can store a floating type value (value with the precision) between the range of ±1.0 x 10-28 to ±7.9228 x 1028. The decimal is an alias of System.Decimal.

It occupies 16 bytes (128 bits) in the memory.

Note: To represent a decimal value, we use m or M as a suffix with the literal.

Syntax

decimal variable_name = value;

It can store the value between the range of ±1.0 x 10-28 to ±7.9228 x 1028.

Example of decimal keyword in C#

Here, we are declaring a decimal variable num, initializing it with the value 5610.2361m and printing its value, type and size of a decimal type variable.

using System;
using System.Text;

namespace ConsoleApplication3 {
  class Program {
    static void Main(string[] args) {
      //char variable declaration
      decimal num = 5610.2361m;

      //printing value
      Console.WriteLine("num: " + num);
      //printing type of variable
      Console.WriteLine("Type of num: " + num.GetType());
      //printing size of a decimal 
      Console.WriteLine("Size of a decimal variable: " + sizeof(decimal));

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

Output

num: 5610.2361
Type of num: System.Decimal
Size of a decimal variable: 16



Comments and Discussions!

Load comments ↻





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