classAnova - Knowledge Shared


Posted by: Bob Cozzi
IT Researcher
Cozzi Productions, Inc.
North Aurora, IL
Bob Cozzi's RPG IV Cheat Sheets
has no ratings.
Published: 08 Feb 2011
Revised: 04 Aug 2011 - 2909 days ago
Last viewed on: 20 Jul 2019 (4460 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

Bob Cozzi's RPG IV Cheat Sheets Published by: Bob Cozzi on 08 Feb 2011 view comments

When using %DEC, you are not allowed to specify %LEN(targetVar) and %DECPOS(targetVar) for the second and third parameters of the %DEC built-in function. The code below illustrates how to use named constants to circumvent this shortcoming.

     D numVar          S              7P 2                                 
     D charVar         S             10A   Inz(' 543.21')                  
                                                                           
     D NUMLEN          C                   Const(%len(numVar))             
     D NUMPOS          C                   Const(%decpos(numVar))          
                                                                           
     C                   EVAL      *INLR = *ON                             
      /free                                                                
                                                                           
          numVar = %dec( charVar : 7 : 2);     // Valid                    
                                                                           
          numVar = %dec( charVar : %Len(numVar) : %decPos(numVar)); // Fail
                                                                           
          numVar = %dec( charVar : NUMLEN : NUMPOS );  // Valid            
                                                                           
          return;                                                          
      /end-free 

As seen on line 14 above, the named constants NUMLEN and NUMPOS may be used when you want the target variable's attributes to influence the %DEC built-in function. Line 12 illustrates an assumed valid syntax, but does not compile. So the code on line 14 is necessary, or worse, the code on line 10 (hard coding the values) may be used.

Return to classanova.com home page.
Sort Ascend | Descend

COMMENTS