Updated on 2024-10-25 GMT+08:00

Decimal Functions and Operators

DECIMAL Literal

You can use the DECIMAL'xxxxxxx.yyyyyyy' syntax to define literals of the DECIMAL type.

The literal precision of the DECIMAL type will be equal to the number of bits of the literal (including trailing zeros and leading zeros). The range will be equal to the number of digits in the decimal part (including trailing zeros).

Example Literal

Data Type

DECIMAL '0'

DECIMAL(1)

DECIMAL '12345'

DECIMAL(5)

DECIMAL '0000012345.1234500000'

DECIMAL(20, 10)

Binary Arithmetic Decimal Operator

Standard mathematical operators are supported. The following table describes the rules for calculating the precision and range of the results. Assume that the type of x is DECIMAL(xp, xs) and the type of y is DECIMAL(yp, ys).

Calculation

Result Type Precision

Result Type Range

x + y and x - y

min(38, 1 + min(xs, ys) + min(xp - xs, yp - ys) )

max(xs, ys)

x * y

min(38, xp + yp)

xs + ys

x / y

min(38, xp + ys + max(0, ys-xs) )

max(xs, ys)

x % y

min(xp - xs, yp - ys) + max(xs, bs)

max(xs, ys)

If the mathematical result of the operation cannot be accurately represented by the precision and range of the result data type, the exception occurs: Value is out of range.

When an operation is performed on a decimal type with different ranges and precisions, the value is first cast to a common supertype. For types close to the maximum representable precision (38), this may cause a "value out of range" error when an operand does not conform to the public supertype. For example, the common supertype of decimal (38, 0) and decimal (38, 1) is decimal (38, 1), but some values that comply with decimal (38, 0) cannot be represented as decimal (38, 1).

Comparison Operators

All standard comparison operators and BETWEEN operators apply to DECIMAL types.

Unary Decimal Operators

The operator "-" performs a negative operation. The type of the result is the same as that of the parameter.