如何检查给定长度的有效数字?

时间:2015-01-27 09:49:27

标签: cobol

如何检查12位数的id-number是数字还是不是数字,如果我的id是10位但我的字段是12位数字,如何检查COBOL中有效的12位id-number?

1 个答案:

答案 0 :(得分:1)

这是一些演示代码和输出。

IDENTIFICATION DIVISION.
PROGRAM-ID. CHECKNUMB.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-ID PIC 9(12).
PROCEDURE DIVISION.
    MOVE 'abc' TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 001234567890 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 1234567890 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 12345678901 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 123456789012 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    GOBACK.

CHECK-CORRECT-12-NUMB SECTION.
    DISPLAY 'checking input: ' INPUT-ID
    IF INPUT-ID IS NUMERIC
        DISPLAY 'is numeric'
        IF INPUT-ID > 99999999999 
            DISPLAY 'correct! 12 digits entered!'
        ELSE
            DISPLAY 'expected 12 digits!'
        END-IF
    ELSE
        DISPLAY 'non numeric entered'
    END-IF
    DISPLAY '-------'
    CONTINUE.

<强>输出

checking input: 000000000abc                                                  
non numeric entered                                                           
-------                                                                       
checking input: 001234567890                                                  
is numeric                                                                    
expected 12 digits!                                                           
-------                                                                       
checking input: 001234567890                                                  
is numeric                                                                    
expected 12 digits!                                                           
-------                                                                       
checking input: 012345678901                                                  
is numeric                                                                    
expected 12 digits!                                                           
-------                                                                       
checking input: 123456789012                                                  
is numeric                                                                    
correct! 12 digits entered!                                                   
-------