Converting a character field to a numeric field is a common occurrence usually accomplished by using data structures and moves. The programmer knows the length of the field and simply moves it into a numeric field of the same size. Writing a program to convert a lengthy character field containing a numeric value of unknown length is a challenge. A left-justified number moved into a numeric field becomes a very large number (the system pads the trailing blanks with zeros).
To accomplish the desired result, the character field has to be moved over in such a way that it will be right justified. I wrote a short program (see 5) that returns a numeric value. The program checks to make sure that each character is a valid digit by accepting it only if it is greater than the letter Z (only numbers 0 through 9 are greater than Z). In this way, the program can handle commas, decimal points, and dollar signs.
To accomplish the desired result, the character field has to be moved over in such a way that it will be right justified. I wrote a short program (see Figure 5) that returns a numeric value. The program checks to make sure that each character is a valid digit by accepting it only if it is greater than the letter Z (only numbers 0 through 9 are greater than Z). In this way, the program can handle commas, decimal points, and dollar signs.
? Andrew Longo
TechTalk: Character-to-Numeric Conversion
Figure 5: Character-to-Numeric Conversion Program
*=============================================================== * To compile: * * CRTRPGPGM PGM(XXX/CVT003RG) SRCFILE(XXX/QRPGSRC) * *=============================================================== *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 E C1 19 1 E N1 19 1 C *ENTRY PLIST C PARM CH 21 C PARM NBR 152 C MOVE *BLANKS DEC 2 C ' ' CHECKCH ST 20 C SUBSTCH:ST CH P C '.' SCAN CH D 20 88 C *IN88 IFEQ *ON C SUB 1 D C Z-ADDD LN C D SUBSTCH WHOLE 19 P C ADD 2 D C 2 SUBSTCH:D DEC C EXSR RTADJ C ELSE C ' ' CHEKRCH LN 20 C LN SUBSTCH WHOLE P C EXSR RTADJ C ENDIF C MOVE *ON *INLR C RTADJ BEGSR C MOVE DEC DEC1 22 C MOVEAWHOLE C1 C CLEARN1 C Z-ADD19 R 20 C Z-ADDLN S 20 C DO LN C C1,S IFGT 'Z' C MOVE C1,S N1,R C SUB 1 R C ENDIF C SUB 1 S C ENDDO C MOVEAN1 WHOLE C MOVE WHOLE NUM1 130 C Z-ADDNUM1 NBR C ADD DEC1 NBR C ENDSR *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7
LATEST COMMENTS
MC Press Online