TechTalk: Character-to-Numeric Conversion

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: