Home > AS/400 Tips > iSeries programmer tips > BIFs simplify numeric to alpha conversions
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

BIFs simplify numeric to alpha conversions


John Kohan, Search400.com expert
03.12.2003
Rating: -2.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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:

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

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*                                                   
I** Break the number into whole number and decimal   
I*                                                   
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*                                                             
C** If the value has a negative sign, place it as a leading "-"
C*                                                 
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.

USER FEEDBACK TO THIS TIP

  • 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

==================================
MORE INFORMATION
==================================

  • 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.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
iSeries CL programming
Taking advantage of CL advancements, starting with V5R3
Checking in on your IBM i authorization lists
Running PHP open source applications: NOBODY needs authority
Simplify the process of converting a spool file from iSeries into an Excel spreadsheet
CL program for daily backups
An automated CL method of moving a query from AS/400 to Excel
Changing user password expiration
Eight steps for creating program documentation using AS/400 utilities
DAYSPAST CLLE program for AS/400: Compares object creation date with today's date
Advanced Job Scheduler help

RPG iSeries programming
Enhancing RPG with external SQL stored procedures
Introduction to SQLRPGLE on IBM i: Making a report
Making the most of RPG data handling on IBM i
IBM i shop boosts online sales with RPG-based Web platform
Migrating from RPG to EGL on IBM i
Allow access to data from a stored procedure result set using COBOL or RPG
EGL Rich UI on IBM i: Do you Dojo?
Programming for the Web on the IBM i, what is possible
A taste of COMMON: ILE, IBM releases, Web applications and new products
Documenting nested program structures on the AS/400

Application Development
iSeries calling an .exe
Top 10 programmer tips
Formatted work job scheduler
Convert system date and time
Mixing free format code with embedded SQL
SQL update a field in one file from a field in another file
Webcasts for iSeries programmers
Programming advice from the pros
Easy code copying via the drag and drop method
Setting FTP time-outs

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Report Program Generator  (Search400.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts