I've written several articles on how to convert numeric data stored in a character field to a true numeric field. But these articles have always referenced RPG IV as the base language--and for good reason: RPG IV is infinitely easier to program in than RPG III could ever hope to be, and RPG IV's expression support does not implicitly convert character data to numeric.
In RPG III and RPG IV, the MOVE opcode implicitly converts data from character to numeric, so there hasn't been much reason to create a routine that would convert numeric data in a character field into a numeric value; just use the MOVE opcode!
But someone recently asked me how to copy numeric data that is stored in a character field along with some garbage characters into a numeric field. Obviously, they want the garbage to be filtered out.
To do this, you need to perform four basic steps.
- Figure out what the garbage characters are.
- Remove the garbage characters from the original character field.
- Right-justify and zero fill the number within the character field.
- Copy it to the target packed or zoned decimal field.
Step 1 is so easy to do that it is purely overlooked. To accomplish this, I set up two named constants as follows:
I ' ' C BLKS
The NUMS constant contains all the numeric digits and the minus sign. The BLKS constant is all blanks.
These two fields are then used with an XLATE opcode to remove the good data from the original source value.
In the following example, I translate the numeric digits, the decimal notation symbol, and the minus sign (if applicable) to blanks. The MYFLD field is the original character field that contains both the numeric value and the garbage I want to remove. The field named NONNUM is the target of the XLATE. It receives the garbage data, along with a few blanks.
.....C..n01n02n03Factor1+++OpCodFactor2+++ResultLenDXHiLoEq
C NUMS:BLKS XLATEMYFLD NONNUM
Next, the garbage must be removed from the numeric field. There are two ways of doing this, depending on the format of the original data.
If the original data contains an integer (whole number) that also contains some garbage either before or after the value, we use a combination of the CHEKR (check right) and CHECK (check left) opcodes to isolate the numeric data. Then, we use substring to extract the digits, leaving them left-justified in the character field.
If the original data contains thousands notation (i.e., commas) in addition to the garbage, you use a combination of concatenation and CHECK and CHEKR to accomplish a similar task. I have not implemented this option in this example.
Once the numeric digits are isolated inside the character field and are left-justified, you need to right-adjust the data and zero fill it on the left side. This is extremely easy by using a DO loop and the CAT opcode.
The full, working example is illustrated below and is written in RPG III. I first wrote it in RPG IV simply because my RPG III skills are evaporating with time. But I did use short field names and avoided expressions. In fact, if I were to convert this RPG III source to RPG IV, it would look essentially the same as it did when I originally wrote it.
|
Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.
LATEST COMMENTS
MC Press Online