Here is an easy way to convert numeric fields to alpha fields when working with data presented to the Web, spreadsheets, text files or other databases where padded zeros become a nuisance. Just use the CHECK opcode and substring the field equal to it, starting at the result of your check operation. A couple of things to remember:
1. Don't forget to check for your index returning a zero if the numeric field is full.
2. If you use free format RPG, you would want to use the %check bif. Also, this code could easily be written in a procedure with variable length fields to handle all of your data conversion needs.
D @Number S 9 0 D @Alpha S 9 D x S 3 0 C move @Number @Alpha C ' 0' check @Alpha x C if x > *zero C eval @Alpha = %subst(@Alpha:x) C else C eval @Alpha = '0' C endif
================================== MORE INFORMATION ON THIS TOPIC ==================================
The Best Web Links: tips, tutorials and more.
Ask your programming questions--or help out your peers by answering them--in our live discussion forums.
Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.
Ask your Web development questions--or help out your peers by answering them--in our live discussion forums.
- This tip does not handle negative numbers correctly. For example, if @Number = -2, @Alpha = 'K'. Not exactly what the spreadsheet user was expecting. RPG already has a BIF for this task: %Char. It will also correctly display a number with decimal places. Why reinvent the wheel? Example: C Eval @Alpha = %Char(@Number) %Char does a good job of formatting numeric, date, time, and timestamp data. If you require more precise control, you can use the %Editc of %Editw BIFs. Example: C Eval @Alpha = %Triml(%Editc(@Number : 'J')) -- Nick Hobson
This was first published in January 2002