ILE C/C++ Programmer's Guide

Converting from a Packed Decimal Type to an Integer Type

When you convert a value of a packed decimal type to an integer type, the value becomes a packed decimal (20,0), which then becomes an integer type. High-order bits will be truncated depending on the size of the integer type. No run-time exception occurs when assigning a packed decimal to an integer type that results in truncation of the integral part.

Examples:

The following example shows the conversion from a packed decimal type that has a fractional part to an integer type.

Figure 276. ILE C Source to Convert a Packed Decimal with a Fractional Part to an Integer




#include <decimal.h>
int main (void)
{
int op;
decimal(7,2) op1 = 12345.67d;
op = op1; /* Truncation on the fractional */
/* part. op=12345 */
}

The following example shows the conversion from a packed decimal type that has less than 10 digits in the integral part to an integer type.

Figure 277. ILE C Source to Convert a Packed Decimal with Less than 10 Digits in the Integral Part to an Integer




#include <decimal.h>
int main(void)
{
int op;
decimal(3) op2=123d;
op = op2; /* No truncation and op=123 */
}

The following example shows the conversion from a packed decimal type that has more than 10 digits in the integral part to an integer type.

Figure 278. ILE C Source to Convert a Packed Decimal with More than 10 Digits in the Integral Part to an Integer




#include <decimal.h>
int main (void)
{
int op2;
decimal(12) op3;
op3 = 123456789012d;
op2 = op3; /* High-order bits will be truncated.*/
/* op2 = 0xBE991A14 */
}

The following example shows conversion from a packed decimal type that has a fractional part, and an integral part having more than 10 digits to an integer type.

Figure 279. ILE C Source to Convert a Packed Decimal with More than 10 Digits in Both Parts to an Integer




#include <decimal.h>
int main (void)
{
int op;
long long op_2;
decimal(15,2) op_1 = 1234567890123.12d;
op = op_1; /* High-order bits will be truncated. */
op_2 = op_1; /* op_2 = 1234567890123, op = 0x71FB04CB */
}


[ Top of Page | Previous Page | Next Page | Table of Contents ]