zen
zen

Reputation: 13

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)))

Upvotes: 1

Views: 620

Answers (2)

Barbara Morris
Barbara Morris

Reputation: 3664

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.

Upvotes: 1

RockBoro
RockBoro

Reputation: 2473

the %editc built in function dates back to the begin of time. So does %len, %trim and varying fields.

** ---------------------- test0003r --------------------------- 
dtest0003r        pi                                            
                                                                
d errmsg          s            256a                             
d packNum         s             15p 0                           
d lx              s             10i 0                           
d v20             s             20a   varying                   
d ch50            s             50a                             
 /free                                                          
      packNum     = 32553;                                      
      v20         = %trim(%editc(packNum:'Z')) ;                
      lx          = %len(v20) ;                                 
      ch50        = %trim(%editc(lx:'Z')) + ' ' + v20 ;         
      dsply       ch50 ;                                        
                                                                
      *inlr       = '1' ;                                       
      return ;                                                  
                                                                
 /end-free                                                      

Upvotes: 2

Related Questions