We can test numberic values in RPG using opcode TESTN, %Check, Monitor/On-Error.
TESTN (Test Numeric)
C TestN DATE 55
C If *In55 = *Off
C Eval Unreadable = 'Y'
C LeaveSr
C EndIf
C TESTN $date 80
C IF *IN80 = *ON
C Move $date DateFld
C ENDIF
C ELSE
C Move *Zeros DateFld
C ENDIF
TESTN is not allowed in free format. Rather than testing the variable before using it, code the usage of the variable in a MONITOR group and handle any errors with ON-ERROR.
Alternatives to TESTN
- %CHECK
- MONITOR
i = %check('0123456789.':A0) ;
/free
01 monitor ;
02 N = %dec(fieldval:8:2) ;
03 on-error ;
04 dsply fieldval ;
05 endmon ;
If FromDate <> *Blanks
and
%Check('0123456789':FromDate)=0;
//Logic
EndIf;
dcl-c $digits const('0123456789');
If %check($digits:wrkFldn) > 0;
// Field is a number, so executing the logic
endif;
If %Check('0123456789' : $Number) = 0;
// Field is a number, so executing the logic
EndIf;
RPGPGM: Validating Numbers without TESTN
%CHECK returns the first position of the string base that contains a character that does not appear in string comparator. If all of the characters in base also appear in comparator, the function returns 0.