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

The decimal Library Functions

The following ESQL/C library functions are available to handle decimal data type numbers.
Function Name Description

decadd()

Adds two decimal numbers

deccmp()

Compares two decimal numbers

deccopy()

Copies a decimal number

deccvasc()

Converts a C char type value to a decimal type value

deccvdbl()

Converts a C double type value to a decimal type value

deccvint()

Converts a C int type value to a decimal type value

deccvlong()

Converts a C long type value to a decimal type value

decdiv()

Divides two decimal numbers

dececvt()

Converts a decimal value to an ASCII string

decfcvt()

Converts a decimal value to an ASCII string

decmul()

Multiplies two decimal numbers

decround()

Rounds a decimal number

decsub()

Subtracts two decimal numbers

dectoasc()

Converts a decimal type value to an ASCII string

dectodbl()

Converts a decimal type value to a C double type value

dectoint()

Converts a decimal type value to a C int type value

dectolong()

Converts a decimal type value to a C long type value

dectrunc()

Truncates a decimal number

When you compile your ESQL/C program with the esql preprocessor, it calls on the link editor to link the library that contains these functions to your program.

Chapter 5, "Working with Numeric Data Types," describes the function rfmtdec(), which allows you to format a decimal number. The following pages describe the functions in the preceding table.

decadd()

The decadd() function adds two decimal type values.

Syntax

Usage

The sum can be the same as either n1 or n2.

Return Codes

0

The operation was successful.

-1200

The operation resulted in overflow.

-1201

The operation resulted in underflow.

Example

The file decadd.ec in the demo directory contains the following sample program.

Example Output

deccmp()

The deccmp() function compares two decimal type numbers.

Syntax

Return Codes

-1

The first value is less than the second value.

0

The two values are identical.

1

The first value is greater than the second value.

DECUNKNOWN

Either value is null.

Example

The file deccmp.ec in the demo directory contains the following sample program.

Example Output

deccopy()

The deccopy() function copies one decimal structure to another.

Syntax

The deccopy() function does not return a status value. To determine the success of the copy operation, look at the contents of the decimal structure to which the target argument points.

Example

The file deccopy.ec in the demo directory contains the following sample program.

Example Output

deccvasc()

The deccvasc() function converts a value held as printable characters in a C char type into a decimal type number.

Syntax

Usage

The character string, strng_val, can contain the following symbols:

The deccvasc() function ignores leading spaces in the character string.

Return Codes

0

The conversion was successful.

-1200

The number is too large to fit into a decimal type structure (overflow).

-1201

The number is too small to fit into a decimal type structure (underflow).

-1213

The string has non-numeric characters.

-1216

The string has a bad exponent.

Example

The file deccvasc.ec in the demo directory contains the following sample program.

Example Output

deccvdbl()

The deccvdbl() function converts a C double type number into a decimal type number.

Syntax

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The file deccvdbl.ec in the demo directory contains the following sample program.

Example Output

deccvint()

The deccvint() function converts a C int type number into a decimal type number.

Syntax

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The file deccvint.ec in the demo directory contains the following sample program.

Example Output

deccvlong()

The deccvlong() function converts a C long type value into a decimal type value.

Syntax

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The file deccvlong.ec in the demo directory contains the following sample program.

Example Output

decdiv()

The decdiv() function divides two decimal type values.

Syntax

Usage

The quotient can be the same as either n1 or n2.

Return Codes

0

The operation was successful.

-1200

The operation resulted in overflow.

-1201

The operation resulted in underflow.

-1202

The operation attempted to divide by zero.

Example

The file decdiv.ec in the demo directory contains the following sample program.

Example Output

dececvt() and decfcvt()

The dececvt() and decfcvt() functions are analogous to the subroutines under ECVT(3) in section three of the UNIX Programmer's Manual. The dececvt() function works in the same fashion as the ecvt(3) function, and the decfcvt() function works in the same fashion as the fcvt(3) function. They both convert a decimal type number to a C char type value.

Syntax

Usage

The dececvt() function converts the decimal value to which np points into a
null-terminated string of ndigit ASCII digits and returns a pointer to the string. A subsequent call to this function overwrites the string.

The dececvt() function rounds low-order digits.

The decfcvt() function is identical to dececvt(), except that ndigit specifies the number of digits to the right of the decimal point instead of the total number of digits.

Let dec_val point to a decimal value of 12345.67 and suppress all arguments except ndigit. The following table shows the values that the dececvt() function returns for four different ndigit values.

ndigit Value Return String *decpt Value *sign

4

"1235"

5

0

10

"1234567000"

5

0

1

"1"

5

0

3

"123"

5

0

For more examples of dec_val and ndigit values, see the sample output of the dececvt.ec demo program on page 4-32.

Warning: When you write thread-safe ESQL/C applications, do not use the dececvt() or decfcvt() library functions. Instead, use their thread-safe equivalents, ifx_dececvt() and ifx_decfcvt(). For more information, see Chapter 13, "Using Informix Libraries."

dececvt() Example

The file dececvt.ec in the demo directory contains the following sample program.

dececvt() Example Output

decfcvt() Example

The file decfcvt.ec in the demo directory contains the following sample program.

decfcvt() Example Output

decmul()

The decmul() function multiplies two decimal type values.

Syntax

Usage

The product can be the same as either n1 or n2.

Return Codes

0

The operation was successful.

-1200

The operation resulted in overflow.

-1201

The operation resulted in underflow.

Example

The file decmul.ec file in the demo directory contains the following sample program.

Example Output

decround()

The decround() function rounds a decimal type number to fractional digits.

Syntax

Usage

The rounding factor is 5 ¥ 10-s-1. To round a value, the decround() function adds the rounding factor to a positive number or subtracts this factor from a negative number. It then truncates to s digits, as the following table shows

Value Before Round Value of s Rounded
Value

1.4

0

1.0

1.5

0

2.0

1.684

2

1.68

1.685

2

1.69

1.685

1

1.7

1.685

0

2.0

.

Example

The file decround.ec in the demo directory contains the following sample program.

Example Output

decsub()

The decsub() function subtracts two decimal type values.

Syntax

Usage

The difference can be the same as either n1 or n2.

Return Codes

0

The operation was successful.

-1200

The operation resulted in overflow.

-1201

The operation resulted in underflow.

Example

The file decsub.ec in the demo directory contains the following sample program.

Example Output

dectoasc()

The dectoasc() function converts a decimal type number to a C char type value.

Syntax

Usage

If right = -1, the decimal value of *np determines the number of decimal places.

If the decimal number does not fit into a character string of length len, dectoasc() converts the number to an exponential notation. If the number still does not fit, dectoasc() fills the string with asterisks. If the number is shorter than the string, dectoasc() left-justifies the number and pads it on the right with blanks.

Because the character string that dectoasc() returns is not null terminated, your program must add a null character to the string before you print it.

Return Codes

0

The conversion was successful.

-1

The conversion failed.

Example

The file dectoasc.ec in the demo directory contains the following sample program.

Example Output

dectodbl()

The dectodbl() function converts a decimal type number into a C double type number.

Syntax

Usage

The floating-point format of the host computer can result in loss of precision in the conversion of a decimal type number to a double type number.

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The file dectodbl.ec file in the demo directory contains the following sample program.

Example Output

dectoint()

The dectoint() function converts a decimal type number into a C int type number.

Syntax

Usage

The dectoint() library function converts a decimal value to a C integer. The size of a C integer depends on the hardware and operating system of the computer you are using. Therefore, the dectoint() function equates an integer value with the SQL SMALLINT data type. The valid range of a SMALLINT is between 32767 and -32767. To convert larger decimal values to larger integers, use the dectolong() library function.

Return Codes

0

The conversion was successful.

<0

The conversion failed.

-1200

The magnitude of the decimal type number is greater than 32767.

Example

The file dectoint.ec in the demo directory contains the following sample program.

Example Output

dectolong()

The dectolong() function converts a decimal type number into a C long type number.

Syntax

Return Codes

0

The conversion was successful.

-1200

The magnitude of the decimal type number is greater than 2,147,483,647.

Example

The file dectolong.ec in the demo directory contains the following sample program.

Example Output

dectrunc()

The dectrunc() function truncates a rounded decimal type number to fractional digits.

Syntax

Usage

The following table shows the sample output from dectrunc() with various inputs.

Value Before Truncation Value of s Truncated Value

1.4

0

1.0

1.5

0

1.0

1.684

2

1.68

1.685

2

1.68

1.685

1

1.6

1.685

0

1.0

Example

The file dectrunc.ec in the demo directory contains the following sample program.

Example Output




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