You Can View User Feedback To This Tip
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.