Start of change

%INT built-in function

%INT converts character, logical, decimal, or unsigned integer data to integer 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 %INT built-in function can be used anywhere that CL supports an arithmetic expression. %INT can be used alone or as part of a more complex arithmetic expression. For example, %INT can be used to compare a character CL variable to a numeric CL variable in the COND parameter of an IF or WHEN command. %INT 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 integer data built-in function is:
%INT(convert-argument)

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

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 result will be in integer format, any decimal digits are truncated without rounding. And the size of the result is always 4 bytes.

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

  • Convert character variable
    DCL VAR(&POINTS) TYPE(*CHAR) LEN(10) VALUE('-123.45')
    DCL VAR(&ANSWER) TYPE(*INT)
    
    /* &ANSWER will have the value -23 */
    CHGVAR VAR(&ANSWER) VALUE(100 + %INT(&POINTS))
  • Convert logical variable
    DCL VAR(&ANSWER1) TYPE(*LGL) VALUE('1')
    DCL VAR(&ANSWER2) TYPE(*LGL) VALUE('1')
    DCL VAR(&NUM) TYPE(*INT)
    
    /* &NUM will have the value 2. */
    CHGVAR VAR(&NUM) VALUE(%INT(&ANSWER1) + %INT(&ANSWER2))
    SNDPGMMSG MSG('The number of YES 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 (%INT(&POINTS1) *EQ %INT(&POINTS2)) +
    THEN(SNDPGMMSG ('The scores are the same!'))
  • Convert unsigned integer variable
    DCL VAR(&P1) TYPE(*INT) LEN(2)
    DCL VAR(&P2) TYPE(*UINT) LEN(2) VALUE(1)
    
    CHGVAR VAR(&P1) VALUE(%INT(&P2))
  • Decimal digits will be truncated without rounding
    DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE('-123.9')
    DCL VAR(&ANSWER) TYPE(*INT)
    
    /* &ANSWER will have the value -123 */
    CHGVAR VAR(&ANSWER) VALUE(%INT(&STRING))
End of change