DECIMAL(p,s)

The DECIMAL data type is provided to handle large numeric values with exact decimal storage.

Syntax

DECIMAL [ ( precision[,scale]) ]
  1. DEC, DECIMAL and NUMERIC are synonyms.
  2. precision defines the number of significant digits (limit is 32, default is 16).
  3. scale defines the number of digits to the right of the decimal point.
  4. When no scale is specified, the data type defines a floating point number.

Usage

The DECIMAL data type must be used for storing number with fractional parts that must be calculated exactly.

DECIMAL variables are initialized to NULL in functions, modules and globals.

The largest absolute value that a DECIMAL(p,s) can store without errors is 10p-s - 10s. The stored value can have up to 30 significant decimal digits in its fractional part, or up to 32 digits to the left of the decimal point.

When you specify both the precision and scale, you define a decimal with a fixed point arithmetic. If the data type declaration specifies a precision but no scale, it defines a floating-point number with precision significant digits. If the data type declaration specifies no precision and scale, the default is DECIMAL(16), a floating-point number with a precision of 16 digits.

DECIMAL values can be converted to strings according to the DBMONEY environment variable (defines the decimal separator).

In ANSI-compliant databases, DECIMAL data types do not provide floating point numbers. When you define a database column as DECIMAL(16), it is equivalent to a DECIMAL(16,0) declaration. You should always specify the scale to avoid mistakes.

When the default exception handler is used, if you try to assign a value larger than the Decimal definition (for example, 12345.45 into DECIMAL(4,2) ), no out of range error occurs, and the variable is assigned with NULL. If WHENEVER ANY ERROR -1226. If you do not use WHENEVER ANY ERROR, the STATUS variable is not set to -1226.

High-precision math functions

A couple of precision math functions are available, to be used with DECIMAL values. These functions have a higher precision as the standard C library functions based on C double data type, which is equivalent to FLOAT:

MAIN
  DEFINE d1 DECIMAL(10,4)
  DEFINE d2 DECIMAL(10,3)
  LET d1 = 1234.4567
  LET d2 = d1 / 3 -- Rounds decimals to 3 digits 
  DISPLAY d1, d2
END MAIN