Converting decimal types to decimal types

If the value of the decimal type to be converted is within the range of values that can be represented exactly, the value of the decimal type is not changed.

If the value of the decimal type to be converted is outside the range of values that can be represented, the value of the decimal type is truncated. Truncation may occur on either the integral part or the fractional part or both.

When truncation occurs on the fractional part, no compile-time message or runtime exception occurs.

When truncation occurs on the integral part, a compile-time message, a runtime exception, or both, are generated as follows:
Note: An explicit cast is used to suppress compile-time messages and runtime exceptions. A runtime exception may occur if any leading nonzero digits are discarded and the operation is not an explicit cast operation.
In the following examples:

Figure 1 shows an example of fractional part that cannot be represented. Conversion of one decimal object to another decimal object with smaller precision involves truncation on the right of the decimal point.

Figure 1. Fractional part cannot be represented
#include <decimal.h>

void func(void);
void dec_func(decimal( 7, 1 ));
decimal( 7, 4 ) x = 123.4567D;
decimal( 7, 1 ) y;
decimal( 7, 1 ) z = 123.4567D;  /* z = 000123.4D <-- No message,    */
                                /*                   No exception   */
void func(void) {
  decimal( 7, 1 ) a = 123.4567D;  /* a = 000123.4D <-- No message,  */
                                  /*                   No exception */
  y = x;            /* y = 000123.4D   <-- No message, No exception */
  y = 123.4567D;    /* y = 000123.4D   <-- No message, No exception */
  dec_func(x);                      /* <-- No message, No exception */
}

Figure 2 shows an example of an integral part that cannot be represented.Conversion of one decimal object to another decimal object with fewer digits involves truncation on the left of the decimal point.

Figure 2. Integral part cannot be represented
void func(void);
void dec_func(decimal( 5, 2 ));
decimal( 8, 2 ) w = 000456.78D;
decimal( 8, 2 ) x = 123456.78D;
decimal( 5, 2 ) y;
decimal( 5, 2 ) z = 123456.78D;     /* <-- Compile-time error        */
decimal( 5, 2 ) z1 = (decimal( 5, 2 )) 123456.78D;
                                    /* z1 = 456.78D <-- No message,  */
                                    /*                  No exception */
void func(void) {
  decimal( 5, 2 ) a = 123456.78D;   /*  <-- Checkout warning         */
                                    /*                 and exception */
  decimal( 5, 2 ) a1 = (decimal( 5, 2 )) 123456.78D;
                                    /* a1 = 456.78D <-- No message,  */
                                    /*                  No exception */
  y = w;           /* y = 456.78D <-- Checkout warning, No exception */
  y = x;                       /* <-- Checkout warning and exception */
  y = 123456.78D;              /* <-- Checkout warning and exception */
  dec_func(x);                 /* <-- Checkout warning and exception */

  y = (decimal( 5, 2 )) w;
                         /* y = 456.78D <-- No message, No exception */
  y = (decimal( 5, 2 )) x;
                         /* y = 456.78D <-- No message, No exception */
  y = (decimal( 5, 2 )) 123456.78D;
                         /* y = 456.78D <-- No message, No exception */
  dec_func((decimal( 5, 2 )) x);
                                     /* <-- No message, No exception */
}