Home | Previous Page | Next Page   Data Manipulation > Using Numeric Data Types > Fixed-Point Data >

Fixed-Point Binary Representations

The DataBlade API provides the following data types to support the binary representations of SQL fixed-point data types.

DataBlade API Data Type SQL Fixed-Point Data Type
mi_decimal, mi_numeric DECIMAL
mi_money MONEY

Both the DECIMAL and MONEY data types use the same internal format to store a fixed-point value. For more information on this format, see Internal Fixed-Point Decimal Format.

DECIMAL Data Type: Fixed-Point Data

When you define a column with the DECIMAL(p,s) data type, the syntax of this definition specifies a fixed-point value for the column. This value has a total of p (<= 32) significant digits (the precision) and s (<= p) digits to the right of the decimal point (the scale).

Tip:
The DECIMAL data type can also declare a floating-point value with the syntax DECIMAL(p). For more information, see DECIMAL Data Type: Floating-Point Data. For a complete description of the DECIMAL data type, see the IBM Informix: Guide to SQL Reference.

The SQL DECIMAL data type holds the internal (binary) format of a decimal value. This value is a computer-independent method that represents numbers of up to 32 significant digits, with valid values in the range 10-129 to 10+125. For more information, see Internal Fixed-Point Decimal Format.

Tip:
The internal format of the DECIMAL data type is often referred to as its binary representation.

The DataBlade API supports the SQL DECIMAL data type with the mi_decimal data type. Therefore, the mi_decimal data type also holds the binary representation of a decimal value. The mi_numeric data type is a synonym for mi_decimal.

Server Only

Values of the mi_decimal data type cannot fit into an MI_DATUM structure. They must be passed by reference within C UDRs.

End of Server Only
Client Only

All data types, including mi_decimal, must be passed by reference within client LIBMI applications.

End of Client Only

MONEY Data Type

When you define a column with the MONEY(p) data type, it has a total of p (<= 32) significant digits (the precision) and a scale of 2 digits.

Global Language Support

The default value that the database server uses for scale is locale-dependent. The default locale specifies a default scale of two. For nondefault locales, if the scale is omitted from the declaration, the database server creates MONEY values with a locale-specific scale. For more information, see the IBM Informix: GLS User's Guide.

End of Global Language Support

You can also specify a scale with the MONEY(p,s) syntax, where s represents the scale. For a complete description of the MONEY data type, see the IBM Informix: Guide to SQL Reference.

Tip:
The internal format of the MONEY data type is often referred to as its binary representation.

The DataBlade API supports the SQL MONEY data type with the mi_money data type. The mi_money data type holds the internal (binary) format of a MONEY value. This binary representation of the MONEY data type has the same structure as the fixed-point DECIMAL data type. For more information, see Internal Fixed-Point Decimal Format.

Server Only

Values of the mi_money data type cannot fit into an MI_DATUM structure. They must be passed by reference within C UDRs.

End of Server Only
Client Only

All data types, including mi_money, must be passed by reference within client LIBMI applications.

End of Client Only

The decimal.h Header File

The decimal.h header file contains definitions for use with the DECIMAL and MONEY data types. This header file defines the following items:

The mitypes.h header file automatically includes decimal.h. In turn, the milib.h header file automatically includes mitypes.h, and mi.h automatically includes milib.h. Therefore, you automatically have access to the dec_t structure, the mi_decimal and mi_money data types, any of the decimal macros, or any of the ESQL/C DECIMAL-type functions when you include mi.h in your DataBlade API module.

Internal Fixed-Point Decimal Format

The DECIMAL and MONEY data types store fixed-point values in an Informix-proprietary internal format: the dec_t structure. This structure holds the internal (binary) format of a DECIMAL or MONEY value, as follows:

#define DECSIZE 16

struct decimal
   {
   short dec_exp;
   short dec_pos;
   short dec_ndgts;
   char  dec_dgts[DECSIZE];
   };

typedef struct decimal dec_t;

This dec_t 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.) Table 14 shows the four parts of the dec_t structure.

Table 14. Fields in the dec_t Structure
Field Description
dec_exp The exponent of the normalized dec_t 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 dec_t type number

The dec_pos 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 dec_t 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 dec_t type number, assuming dec_dgts[0] != 0

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

Table 15 shows some sample dec_t values.

Table 15. Sample Decimal Values
Value dec_t 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

The mi_decimal and mi_money data types use the dec_t structure to hold the binary representation of a DECIMAL and MONEY value, respectively.

The Decimal Macros

The decimal.h header file also includes the following macros that might be useful in a DataBlade API module.

Decimal Macro
Description
DECLEN(p, s)
Calculates the minimum number of bytes required to hold the DECIMAL(p,s) value
DECPREC(size)
Calculates a default precision given the number of bytes (size) used to store the number
PRECTOT(dec)
Returns the total precision of the dec value
PRECDEC(dec)
Returns the scale of the dec value
PRECMAKE(p, s)
Creates a precision value from the specified total precision (p) and scale (s)
Tip:
For a complete list of decimal macros, consult the decimal.h header file that is installed with your database server. This header file resides in the incl/public subdirectory of the INFORMIXDIR directory.
ESQL/C DECIMAL-Type Functions

Because the binary representation of DECIMAL (mi_decimal) and MONEY (mi_money) values is an Informix-proprietary format, you cannot use standard system functions to perform decimal operations on mi_decimal and mi_money values. Instead, the DataBlade API provides support for the following ESQL/C functions on the DECIMAL and MONEY data types.

Type of DECIMAL Function More Information
Conversion functions ESQL/C Functions for Decimal Conversion
Arithmetic-operation functions Performing Operations on Decimal Data

Any other operations, modifications, or analyses can produce unpredictable results.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]