Decimal Data Type in C#.NET
For financial and monetary calculations, double data type was not enough so decimal data type was created with higher precision to represent big positive and negative numbers.
The below table provides the size, range, default value and data suffix of decimal data type.
||-7.9 X 10^28 to +7.9 X 10^28
Example of Decimal data type in C#
public class MainClass
public static void Main()
decimal a = 1; // Line 1
decimal b = 1.1; // Line 2
decimal c = 1.1f; // Line 3
decimal d = 1.1d; // Line 4
decimal e = 1.1m; // Line 5
decimal f = 100; // Line 6
Note1: The integral data types are implicitly converted to decimal and the result evaluates to decimal. Therefore you can initialize a decimal variable using an integer literal without the suffix as in line 1 and 6 in above example.
Note2: There is no implicit conversion between floating-point types and the decimal type, therefore a cast must be used to convert between these two types.
For example line 2, line 3 and line 4 in above example are invalid. They must be written as:
decimal b = (decimal)1.1; //There is no suffix so considered as double.
decimal c = (decimal)1.1f;
decimal d = (decimal)1.1d;