我需要将存储为Text数据类型(左对齐)的数字转换为大数帧中的数字格式(右对齐),范围从2到4位。
For eg,
Field1(Text left aligned) Field2(Numeric Right aligned)
---------------------------------------------------------------
1000 1000
11 11
200 200
可以使用SORT EZTRIEVE COBOL。
答案 0 :(得分:2)
对于COBOL解决方案,请尝试使用UNSTRING
和JUSTIFIED RIGHT
接收字段。这是一个示例程序(IBM Enterprise COBOL)来说明。
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
.
该程序的输出是:
SOURCE >123 <
DEST > 123<
SOURCE >1 <
DEST > 1<
答案 1 :(得分:1)
这也有效......
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
.
答案 2 :(得分:0)
我要求与其他两个答案不同,因为他们IMO只是将源文本重新表示为编辑的字母数字输出。这是我的抨击:
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
.
DEST-DATA-N字段具有源数据的真实数字版本。
答案 3 :(得分:0)
比NealB更“冗长”的答案,减少对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 '<'
.
给出了:
SOURCE >1234<DEST >1234<
SOURCE >123 <DEST >0123<
SOURCE >12 <DEST >0012<
SOURCE >1 <DEST >0001<
SOURCE > <DEST >0000<