ILE C/C++ Programmer's Guide

Converting from a Packed Decimal Type to a Packed Decimal Type

The following example illustrates different conversions from packed decimal types to packed decimal types that have different sizes. If the value of the packed decimal type to be converted is not within the range of values that can be represented exactly, the value of the packed decdimal type is truncated. If truncation occurs in the fractional part, the result is truncated, and there is no run-time error.

Figure 272. ILE C Source to Convert Packed Decimals




#include <decimal.h>
int main (void)
{
decimal(4,2) targ_1, targ_2;
decimal(6,2) op_1=1234.56d, op_2=12.34d;
targ_1=op_1; /* A run-time error is generated because the integral
part is truncated; targ_1=34.56d. */
targ_2=op_2; /* No run-time error is generated because neither the
integral nor the fractional part is truncated;
targ_2=12.34d. */
}

If assignment causes truncation in the integral part, then there is a run-time error. A run-time exception occurs when an integral value is lost during conversion to a different type, regardless of what operation requires the conversion. See Understanding Packed Decimal Data Type Errors for an example of run-time exceptions.

Examples:

There is no warning or error during compilation on assignment to a smaller target. See Understanding Packed Decimal Data Type Errors for information on compile-time and run-time errors during conversion.

The following example shows conversion from one packed decimal type to another with a smaller precision. Truncation on the fractional part results.

Figure 273. ILE C Source to Convert a Packed Decimal to a Packed Decimal with Smaller Precision




#include <decimal.h>
int main(void)
{
decimal(7,4) x = 123.4567D;
decimal(7,1) y;
y = x; /* y = 123.4D */
}

The next example shows conversion from one packed decimal type to another with a smaller integral part. Truncation on the integral part results. The #pragma nosigtrunc directive turns off exceptions generated because of overflow.

Figure 274. ILE C Source to Convert a Packed Decimal to a Packed Decimal with Smaller Integral Part




#pragma nosigtrunc
#include <decimal.h>
int main (void)
{
decimal(8,2) x = 123456.78D;
decimal(5,2) y;
y = x; /* y = 456.78D */
}

The next example shows conversion from one packed decimal type to another with a smaller integral part and smaller precision. Truncation on both integral and fractional parts results. The #pragma nosigtrunc directive turns off exceptions generated because of overflow.

Figure 275. ILE C Source to Convert a Packed Decimal to a Packed Decimal with Smaller Integral Part and Smaller Precision




#pragma nosigtrunc
#include <decimal.h>
int main (void)
{
decimal(8,2) x = 123456.78D;
decimal(4,1) y;
y = x; /* y = 456.7D */
}


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