%DECH Examples

Figure 1. Using Numeric and Character Parameters
 *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
D*Name++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D p7              s              7p 3 inz (1234.567)
D s9              s              9s 5 inz (73.73442)
D f8              s              8f   inz (123.456789)
D c15a            s             15a   inz (' 123.456789 -')
D c15b            s             15a   inz (' + 9 , 8 7 6 ')
D result1         s             15p 5
D result2         s             15p 5
D result3         s             15p 5

    // using numeric parameters     
    result1 = %dec (p7) + 0.011;  // "result1" is now 1234.57800
    result2 = %dec (s9 : 5: 0);   // "result2" is now   73.00000
    result3 = %dech (f8: 5: 2);   // "result3" is now  123.46000
    // using character parameters
    result1 = %dec (c15a: 5: 2);  // "result1" is now -123.45
    result2 = %dech(c15b: 5: 2);  // "result2" is now    9.88000

Handling Currency Symbols and Thousands Separators

If the character data is known to contain non-numeric characters such as thousands separators (for example, '1,234,567') or leading asterisks and currency symbols (for example, '$***1,234,567.89'), some preprocessing may be necessary to remove these characters from the data.

However, if Control keyword EXPROPTS(*USEDECEDIT) is specified, the thousands separators indicated by the DECEDIT keyword are considered to be part of the numeric data.

In the following example, the %XLATE built-in function is used to replace any symbol, asterisks or thousands separators with blanks.


D data            s             20a   inz('$1,234,567.89')
D num             s             21p 9
    num = %dech(%xlate('$*,' : '   ' : data)
              : 21 : 9);

In the following example, Control keyword EXPROPTS(*USEDECEDIT) is specified, so it is not necessary to replace the thousands separators with blanks. In the previous example, the first operand of %XLATE, '$*,', contains a comma (,), but in the following example, the first operand of %XLATE is simply '$*'.


H EXPROPTS(*USEDECEDIT)
D data            s             20a   inz('$1,234,567.89')
D num             s             21p 9
    num = %dech(%xlate('$*' : '   ' : data)
              : 21 : 9);

In the following example, the currency symbol or thousands separator might vary at runtime, so variables are used to hold these values.


    num = %dech(%xlate(cursym + '*' + thousandsSep : '   ' : data)
              : 21 : 9);