Decimal variable type, unlike float and double, which are binary point types, is a floating decimal point type. It can store 128 bits, and compared to the float or double types, it has more precision and a smaller range. This makes it best suitable for financial calculations and any situations where precision is crucial. The decimal variable type can store values between 7.9E-28 and 7.9E+28
If you want a numeric real literal to be treated as decimal, use the suffix m or M, for example:
1 |
decimal myDecimalVariable = 157.39M; |
Without the suffix m, the number is treated as a double and generates a compiler error.
The concepts explained in this lesson are also shown visually as part of the following video:
EXERCISES
1. Which of the following values can be assigned to variables of type float, double and decimal: 5, -5.01, 34.567839023; 12.345; 8923.1234857; 3456.091124875956542151256683467?
Solution
Consider the number of digits after the decimal point. Refer to the table that describes the sizes of the types float, double and decimal.
Tags: decimal variable, variable types, variables