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.
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:
- precision_digits <= significant_digits
- 1 <= significant_digits <=
DEC_DIG
- 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.
decimal(10,2) x;
decimal(5,0) y;
decimal(5) z;
decimal(18,10) *ptr;
decimal(8,2) arr[100];
x
can have values between -99999999.99D and +99999999.99D.y
andz
can have values between -99999D and +99999D.ptr
is a pointer to typedecimal(18,10)
.arr
is an array of 100 elements, where each element is of typedecimal(8,2)
.