Start of change

%DEC built-in function

%DEC converts character, logical, decimal, integer, or unsigned integer data to packed decimal format. The converted value can be assigned to a CL variable, passed as a numeric constant to another program or procedure, or specified as a value for a command parameter of a CL command run from compiled CL.

The %DEC built-in function can be used anywhere that CL supports an arithmetic expression. %DEC can be used alone or as part of a more complex arithmetic expression. For example, %DEC can be used to compare a character CL variable to a numeric CL variable in the COND parameter of an IF or WHEN command. %DEC can also be used to set the value of a CL command parameter, if the associated command object defines the parameter with EXPR(*YES) and TYPE of *DEC, *INT2, *INT4, *UINT2, or *UINT4.

The format of the convert to packed decimal data built-in function is:
%DEC(convert-argument [total-digits decimal-places])

The convert-argument must be a CL variable with TYPE of *CHAR, *LGL, *DEC, *INT or *UINT.

Parameters total-digits and decimal-places may be omitted. If these parameters are omitted, for character data, the value of total digits defaults to 15, the value of decimal places defaults to 5; for logical data, the value of total digits defaults to 1, the value of decimal places defaults to 0; for numeric data, the total digits and decimal places are taken from the attributes of the numeric data. The total digits and decimal places, if specified, must be integer literals. The total-digits can be a value from 1 to 15. The decimal-places can be a value from zero to 9 and cannot be greater than the total-digits value.

If the convert-argument parameter is a character variable, the following rules apply:
  • The sign is optional. It can be '+' or '-'. It can precede or follow the numeric data.
  • The decimal point is optional. It can be either a period or a comma.
  • Leading and trailing blanks are allowed in the data. For example, ' +3 ' is a valid parameter.
  • All-blank value will return a zero value.
  • If invalid numeric data is found, an exception occurs with CPF0818.

The following are examples of using the %DEC built-in function:

  • Convert character variable
    DCL VAR(&POINTS) TYPE(*CHAR) LEN(10) VALUE('-123.45')
    DCL VAR(&ANSWER) TYPE(*DEC) LEN(10 5)
    
    /* &ANSWER will have the value -00023.45000 */
    CHGVAR VAR(&ANSWER) VALUE(100 + %DEC(&POINTS 5 2))
  • Convert logical variable
    DCL VAR(&ANSWER1) TYPE(*LGL) VALUE('1')
    DCL VAR(&ANSWER2) TYPE(*LGL) VALUE('1')
    DCL VAR(&NUM) TYPE(*DEC) LEN(5 0)
    
    /* &NUM will have the value 00002. */
    CHGVAR VAR(&NUM) VALUE(%DEC(&ANSWER1 1 0) + %DEC(&ANSWER2))
    SNDPGMMSG MSG('The number of YES answers is' *BCAT %CHAR(&NUM)) 
  • Convert packed decimal variable
    DCL VAR(&POINTS1) TYPE(*DEC) LEN(5 2) VALUE(100.23)
    DCL VAR(&POINTS2) TYPE(*DEC) LEN(5 2) VALUE(100.45)
    
    IF (%DEC(&POINTS1 3 0) *EQ %DEC(&POINTS2 3 0)) +
    THEN(SNDPGMMSG ('The scores are the same!'))
  • Convert integer or unsigned integer variable
    DCL VAR(&P1) TYPE(*INT) LEN(2) VALUE(-1)
    DCL VAR(&P2) TYPE(*UINT) LEN(2) VALUE(1)
    DCL VAR(&NUM) TYPE(*DEC) LEN(5 0)
    
    /* &NUM will have the value 00000. */
    CHGVAR VAR(&NUM) VALUE(%DEC(&P1 10 0)+%DEC(&P2 5 2))
  • Extra decimal digits will be truncated without rounding
    DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE('-123.567')
    DCL VAR(&VALUE) TYPE(*DEC) LEN(7 2)
    
    /* &VALUE will have the value -00123.56 */
    CHGVAR VAR(&VALUE) VALUE(%DEC(&STRING 7 2))
  • All-blank value will return a zero value
    DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE('          ')
    DCL VAR(&VALUE) TYPE(*DEC) LEN(7 2)
    
    /* &VALUE will have the value 00000.00 */
    CHGVAR VAR(&VALUE) VALUE(%DEC(&STRING 7 2))
End of change