decimal keyword in C#
Last Updated :
22 Jun, 2020
Keywords are the words in a language that are used for some internal process or represent some predefined actions.
decimal is a keyword that is used to declare a variable that can store a floating type value from the range of ±1.0 x 10-28 to ±7.9228 x 1028. It is an alias of System.Decimal and occupies 16 bytes (128 bits) in the memory.
Syntax:
decimal variable_name = value;
We have to use ‘m’ or ‘M’ as a suffix with the literal, to represent a decimal value.
Example:
Input: 7271.6521M
Output: num: 7271.6521
Size of a decimal variable: 16
Input: -371.83436
Output: Type of num: System.Decimal
num: -371.83436
Size of a decimal variable: 16
Example 1:
using System;
using System.Text;
class GFG {
static void Main( string [] args)
{
decimal num1 = 7271.6521M;
Console.WriteLine( "num: " + num1);
Console.WriteLine( "Size of a decimal variable: " + sizeof ( decimal ));
}
}
|
Output:
num: 7271.6521
Size of a decimal variable: 16
Example 2:
using System;
using System.Text;
namespace geeks {
class GFG {
static void Main( string [] args)
{
decimal num1 = -371.83436m;
Console.WriteLine( "Type of num: " + num1.GetType());
Console.WriteLine( "num: " + num1);
Console.WriteLine( "Size of a decimal variable: " + sizeof ( decimal ));
Console.ReadLine();
}
}
}
|
Output:
Type of num: System.Decimal
num: -371.83436
Size of a decimal variable: 16
Share your thoughts in the comments
Please Login to comment...