rpglerpg

RPGLE code to RPG - Count number of Digits


I am having an issue with coding this, I need some similar code with this but written in RPG to count the digits in a number.

NumField (15, 0) packed decimal

EVAL numDig = %len(%trim(%char(NumField)))


Solution

  • A fun challenge.

    @zen, I agree with others. I would not try to code this in RPG III. I would either convert the RPG III program to RPG IV, or I would call an RPG IV program to do this calculation.

    But here is some RPG III code that gets the number of digits. It is horrible code, and almost completely untested. I would never use this code or recommend that anyone else use this code.

     C                     Z-ADD1.2       NUM     52
     C                     EXSR SUB1
     C                     Z-ADD-123.45   NUM     52
     C                     EXSR SUB1
     C                     Z-ADD0         NUM     52
     C                     EXSR SUB1
     C                     RETRN
     C           SUB1      BEGSR
     C                     MOVELNUM       STR    30 P
     C           '0':' '   XLATESTR       STR2   30 P
     C           ' '       CHECKSTR2      P       50
     C           P         IFGT 0
     C                     SUBSTSTR2:P    STR3   30 P
     C           ' '       CHEKRSTR3      P       50
     C                     ENDIF
     C           P         DSPLY
     C                     ENDSR
    

    It displays 2, 5, 0.