BIFs simplify numeric to alpha conversions

John Kohan, Search400.com expert

You Can View User Feedback To This Tip

John Kohan

There are times when you want to insert a numeric result into the middle of some text. If you are adjusting a customer's account, you may want to display some text like the following: "A credit of $5.25 was applied to your account."

In RPG IV, the process can be accomplished via a Built-in Function (BIF). The %CHAR BIF will convert the value from numeric to alpha and remove the leading zeros. The following example is one way to use the %CHAR BIF:

result = 'A Credit of $' + %char(@NUMBR) + ' was applied to your account.';

The result is "A credit of $5.25 was applied to your account."

This BIF greatly reduces the amount of code needed to produce the same result in RPG/400. Although many shops have adopted RPG IV, I have provided a sample of RPG/400 code that would produce the same result.

E                    SEV         7  1                
I** Break the number into whole number and decimal   
I            DS                                      
I                                        1   92@NBR  
I                                        1   70@WHOLE
I                                        8   90@DECMN
I              'A Credit Of $'       C         @LEAD1
I              'was applied to your -C         @LEAD2
I              'account'                             
C                     MOVE *ON       *INLR           
C* Load the number                                   
C                     Z-ADD5.25      @NBR            
C* Clear the array                                   
C                     CLEARSEV                       
C* Convert the number to alpha                                 
C                     MOVE @WHOLE    @SEVEN  7 P               
C                     MOVEA@SEVEN    SEV                       
C* Remove the leading zeros and right justify                  
C           1         DO   7         Y       30                
C           SEV,Y     IFEQ '0'                                 
C                     MOVEASEV,2     @SEVEN  7 P               
C                     MOVEA@SEVEN    SEV,1                     
C                     SUB  1         Y                         
C                     ELSE                                     
C                     LEAVE                                    
C                     ENDIF                                    
C                     ENDDO                                    
C* Place the result into an alpha field                        
C                     MOVEASEV       RESLT   7                 
C** If the value has a negative sign, place it as a leading "-"
C           @DECMN    IFLT *ZEROS                  
C                     MULT -1        @DECMN        
C                     MOVEL@DECMN    @DECM   2 P   
C           '-'       CAT  RESLT:0   RESLT         
C                     ELSE                         
C                     MOVEL@DECMN    @DECM     P   
C                     ENDIF                        
C* Put it all together now                         
C           RESLT     CAT  '.':0     CVTNBR 10 P   
C                     CAT  @DECM:0   CVTNBR 10 P   
C           @LEAD1    CAT  CVTNBR:1  RESULT 60 P   
C                     CAT  @LEAD2:1  RESULT        

The result is "A credit of $5.25 was applied to your account."

As you can see, the %CHAR BIF greatly reduces the amount of code required to perform the same task.

The IBM iSeries information center contains the BIFs that are currently supported. Take some time to review these BIFs; they may add value to your processes.

About the author: John Kohan is a senior programmer analyst at CT Codeworks.


  • A much simpler way would be to use the %editc function. Example: EVAL DSPTOT = 'DISPLAY THIS NUMBER ' + %TRIML(%EDITC(TOTFLD:'J')) + ' ON THE SCREEN' —Robert Davis


  • In search of a better Lookup?
    The way RPG OpCode LOOKUP searches a sorted array can take you days to find what you're looking for. In this tip user Nick Hobson offers some other ways to do searches, including using the %Lookup BIF.

  • Check & run commands in RPG
    Sometimes you need to ask the user for a command or simply run a command saved previously. This function, provided by Search400.com member Victor Roig, gives you the option in a very simply way.

This was first published in March 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.