I need to convert Numbers stored as Text datatype(left aligned) ranging from 2 to 4 digits to Numeric format(Right aligned) in Mainframes.
For eg,
Field1(Text left aligned) Field2(Numeric Right aligned)
-----------------------------------开发者_StackOverflow中文版----------------------------
1000 1000
11 11
200 200
Can use SORT EZTRIEVE COBOL.
For a COBOL solution, try using UNSTRING
and a JUSTIFIED RIGHT
receiving field. Here is an example program (IBM Enterprise COBOL) to illustrate.
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SOURCE-DATA PIC X(4).
01 DEST-DATA PIC X(4) JUSTIFIED RIGHT.
PROCEDURE DIVISION.
MOVE '123' TO SOURCE-DATA
UNSTRING SOURCE-DATA DELIMITED BY SPACE
INTO DEST-DATA
DISPLAY 'SOURCE >' SOURCE-DATA '<'
DISPLAY 'DEST >' DEST-DATA '<'
MOVE '1' TO SOURCE-DATA
UNSTRING SOURCE-DATA DELIMITED BY SPACE
INTO DEST-DATA
DISPLAY 'SOURCE >' SOURCE-DATA '<'
DISPLAY 'DEST >' DEST-DATA '<'
GOBACK
.
The output from this program is:
SOURCE >123 <
DEST > 123<
SOURCE >1 <
DEST > 1<
This works as well...
IDENTIFICATION DIVISION.
PROGRAM-ID. STR2NUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SOURCE-DATA PIC X(4).
01 DEST-DATA PIC BBB9.
PROCEDURE DIVISION.
MOVE '123' TO SOURCE-DATA
UNSTRING SOURCE-DATA DELIMITED BY SPACE
INTO DEST-DATA
DISPLAY 'SOURCE >' SOURCE-DATA '<'
DISPLAY 'DEST >' DEST-DATA '<'
MOVE '1' TO SOURCE-DATA
UNSTRING SOURCE-DATA DELIMITED BY SPACE
INTO DEST-DATA
DISPLAY 'SOURCE >' SOURCE-DATA '<'
DISPLAY 'DEST >' DEST-DATA '<'
GOBACK
.
I'll beg to differ with the other two answers as they, IMO, are simply re-representing the source text as edited alphanumeric output. Here's my stab at it:
IDENTIFICATION DIVISION.
PROGRAM-ID. STR2NUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VARIABLES.
05 SRCE-DATA PIC X(4).
05 SRCE-DATA-R PIC X(4) JUST RIGHT.
05 DEST-DATA-N PIC 9(4).
05 DEST-DATA PIC ZZZ9.
PROCEDURE DIVISION.
INITIALIZE WS-VARIABLES
MOVE '11' TO SRCE-DATA
UNSTRING SRCE-DATA DELIMITED BY SPACE
INTO SRCE-DATA-R
INSPECT SRCE-DATA-R
REPLACING ALL SPACES BY ZEROES
MOVE SRCE-DATA-R TO DEST-DATA-N, DEST-DATA
DISPLAY 'SOURCE >' SRCE-DATA '<'
DISPLAY 'DEST >' DEST-DATA '<'
GOBACK
.
The DEST-DATA-N field has a true numeric version of the source data.
A more "verbose" answer than NealB's, less strain on the CPU.
IDENTIFICATION DIVISION.
PROGRAM-ID. STR2NUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SOURCE-DATA PIC X(4).
88 SD-NO-DATA VALUE SPACE.
01 FILLER REDEFINES SOURCE-DATA.
05 SOURCE-DATA-ALL PIC 9(4).
01 FILLER REDEFINES SOURCE-DATA.
05 SD-LEADING-1 PIC 9.
05 SD-TRAILING-3 PIC X(3).
88 SD-DATA-1-LEADING VALUE SPACE.
01 FILLER REDEFINES SOURCE-DATA.
05 SD-LEADING-2 PIC 99.
05 SD-TRAILING-2 PIC XX.
88 SD-DATA-2-LEADING VALUE SPACE.
01 FILLER REDEFINES SOURCE-DATA.
05 SD-LEADING-3 PIC 9(3).
05 SD-TRAILING-1 PIC X.
88 SD-DATA-3-LEADING VALUE SPACE.
01 DEST-DATA PIC 9(4).
PROCEDURE DIVISION.
MOVE '1234' TO SOURCE-DATA
PERFORM PROCESS-THE-DATA
MOVE '123' TO SOURCE-DATA
PERFORM PROCESS-THE-DATA
MOVE '12' TO SOURCE-DATA
PERFORM PROCESS-THE-DATA
MOVE '1' TO SOURCE-DATA
PERFORM PROCESS-THE-DATA
MOVE SPACE TO SOURCE-DATA
PERFORM PROCESS-THE-DATA
GOBACK
.
PROCESS-THE-DATA.
EVALUATE TRUE
WHEN SD-NO-DATA
MOVE ZERO TO DEST-DATA
WHEN SD-DATA-1-LEADING
MOVE SD-LEADING-1 TO DEST-DATA
WHEN SD-DATA-2-LEADING
MOVE SD-LEADING-2 TO DEST-DATA
WHEN SD-DATA-3-LEADING
MOVE SD-LEADING-3 TO DEST-DATA
WHEN OTHER
MOVE SOURCE-DATA-ALL TO DEST-DATA
END-EVALUATE
DISPLAY 'SOURCE >' SOURCE-DATA '<'
'DEST >' DEST-DATA '<'
.
Which gives:
SOURCE >1234<DEST >1234<
SOURCE >123 <DEST >0123<
SOURCE >12 <DEST >0012<
SOURCE >1 <DEST >0001<
SOURCE > <DEST >0000<
精彩评论