Get rid of padded zeros in your e-business apps

You Can View User Feedback To This Tip

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  


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.

The Best Web Links on Web Development.


  • 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

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.