Fixed point decimal types (C only)

Fixed point decimal types are classified as arithmetic types. To declare fixed point decimal variables and initialize them with fixed point decimal constants, you use the type specifier decimal. For this type specifier, decimal is a macro that is defined in the decimal.h header file. Remember to include decimal.h if you use fixed point decimals in your program.

Fixed point decimal syntax

Read syntax diagramSkip visual syntax diagramdecimal(significant_digits ,precision_digits )

The significant_digits is a positive integral constant expression. The second argument, precision_digits is optional. If you leave it out, the default value is 0. The type specifiers decimal(n,0) and decimal(n) are type-compatible.

In the type specifier, significant_digits and precision_digits have a range of allowed values according to the following rules:

  1. precision_digits <= significant_digits
  2. 1 <= significant_digits <= DEC_DIG
  3. 0 <= precision_digits <= DEC_PRECISION

The decimal.h file defines DEC_DIG (the maximum number of digits) and DEC_PRECISION (the maximum precision). Currently, it uses a maximum of 31 digits for both limits.

The following examples show how to declare a variable as a fixed point decimal data type:
decimal(10,2)  x;
decimal(5,0)   y;
decimal(5)     z;
decimal(18,10) *ptr;
decimal(8,2)   arr[100];
In the previous example:
  • x can have values between -99999999.99D and +99999999.99D.
  • y and z can have values between -99999D and +99999D.
  • ptr is a pointer to type decimal(18,10).
  • arr is an array of 100 elements, where each element is of type decimal(8,2).