ILE C/C++ Programmer's Guide

Converting from a Packed Decimal Type to a Floating Point Type

When a value of packed decimal type is converted to floating type, if the value being converted is outside the range of values that can be represented, then the behavior is undefined. If the value being converted is within the range of values that can be represented, but cannot be represented exactly, the result is truncated. When a float or a double is converted to a packed decimal with smaller precision, the fractional part of the float or the double will be truncated.

The following example shows the conversion from a packed decimal type to a floating point type.

Figure 280. ILE C Source to Convert a Packed Decimal to a Floating Point




#include <decimal.h>
#include <stdio.h>
int main(void)
{
decimal(5,2) dec_1=123.45d;
decimal(11,5) dec_2=-123456.12345d;
float f1,f2;
f1=dec_1;
f2=dec_2;
printf("f1=%f\nf2=%f\n\n",f1,f2); /* f1=123.449997 */
/* f2=-123456.125000 */
}

The output is as follows:

+--------------------------------------------------------------------------------+
|  f1=123.449997                                                                 |
|  f2=-123456.125000                                                             |
|  Press ENTER to end terminal session.                                          |
+--------------------------------------------------------------------------------+


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