INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 4: Working with the DECIMAL Data Type
Home Contents Index Master Index New Book

The SQL DECIMAL Data Type

ESQL/C supports the SQL DECIMAL data type with the decimal data type. The decimal data type is a machine-independent method that represents numbers of up to 32 significant digits, with valid values in the range 10-129 to 10+125.

The DECIMAL data type can take the following two forms:

    When you define a column with the DECIMAL(p) data type, it has a total of p (< = 32) significant digits. DECIMAL(p) has an absolute value range between 10-130 and 10124.

    When you define a column with the DECIMAL(p,s) data type, it has a total of p (< = 32) significant digits (the precision) and s (< = p) digits to the right of the decimal point (the scale).

For a complete description of the DECIMAL data type, see the Informix Guide to SQL: Reference.

The decimal Data Type

Use the decimal data type to declare host variables for database values of type DECIMAL. A structure of type decimal represents a value in a decimal host variable, as follows:

The decimal.h header file contains the decimal structure and the typedef dec_t. Include this file in all C source files that use any decimal host variables with the following include directive:

The decimal structure stores the number in pairs of digits. Each pair is a number in the range 00 to 99. (Therefore, you can think of a pair as a base-100 digit.) Figure 4-1 shows the four parts of the decimal structure.

Figure 4-1
Fields in the decimal Structure

Field Description

dec_exp

The exponent of the normalized decimal type number. The normalized form of this number has the decimal point at the left of the left-most digit. This exponent represents the number of digit pairs to count from the left to position the decimal point (or as a power of 100 for the number of base-100 numbers).

dec_pos

The sign of the decimal type number. The dec_pos field can assume any one of the following three values:

1: when the number is zero or greater
0: when the number is less than zero
-1: when the value is null

dec_ndgts

The number of digit pairs (number of base-100 significant digits) in the decimal type number. This value is also the number of entries in the dec_dgts array.

dec_dgts[]

A character array that holds the significant digits of the normalized decimal type number, assuming dec_dgts[0] ! = 0.

Each byte in the array contains the next significant base-100 digit in the decimal type number, proceeding from dec_dgts[0] to dec_dgts[dec_ndgts].

Figure 4-2 shows some sample decimal values.

Figure 4-2
Sample decimal Values

Value decimal Structure Field Values
dec_exp dec_pos dec_ndgts dec_dgts[]

-12345.6789

3

0

5

dec_dgts[0] = 01

dec_dgts[1] = 23

dec_dgts[2] = 45

dec_dgts[3] = 67

dec_dgts[4] = 89

1234.567

2

1

4

dec_dgts[0] = 12

dec_dgts[1] = 34

dec_dgts[2] = 56

dec_dgts[3] = 70

-123.456

2

0

4

dec_dgts[0] = 01

dec_dgts[1] = 23

dec_dgts[2] = 45

dec_dgts[3] = 60

480

2

1

2

dec_dgts[0] = 04

dec_dgts[1] = 80

.152

0

1

2

dec_dgts[0] = 15

dec_dgts[1] = 20

-6

1

0

1

dec_dgts[0] = 06

You can use the deccvasc demonstration program to experiment with how ESQL/C stores decimal numbers.

Implicit Data Conversion for decimal Values

All operations on decimal type numbers take place through the functions that the ESQL/C library provides. Any other operations, modifications, or analyses can produce unpredictable results. ESQL/C provides functions that facilitate conversion of decimal data type numbers to and from each
C-language data type. The following section describes these functions.

Operations on decimal Values

The ESQL/C decimal data type uses its own internal structure to store decimal values. (For more information, see "The decimal Data Type".) Use the following ESQL/C library functions to perform all operations on decimal type numbers.

decadd()

deccopy()

decmul()

decsub()

deccmp()

decdiv()

decround()

dectrunc()

Any other operations, modifications, or comparisons can produce unpredictable results. For more information on how to use these functions, refer to "The decimal Library Functions".

Data Conversion for decimal Values

ESQL/C provides the following library functions to support conversion of decimal values to and from C language data types.

deccvasc()

deccvlong()

dectoasc()

dectoint()

deccvdbl()

dececvt()

dectodbl()

dectolong()

deccvint()

decfcvt()

For more information on how to use these functions, refer to "The decimal Library Functions".

You can control the number of decimal digits that ESQL/C places in character host variables with an internal ESQL/C global variable that is called dbfltprec, or with the DBFLTMASK environment variable. For more information, see "Operations That Involve a Decimal Value".




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.