How to replace spaces at the right into zeros at the left in COBOL?

后端 未结 5 702
孤街浪徒
孤街浪徒 2021-01-22 09:39

I have an alphanumeric variable with a length of 10. It contains a number at the beginning, the rest of the digits are filled with spaces. Then I need to move the string to the

5条回答
  •  面向向阳花
    2021-01-22 10:21

    If you have intrinsics, shuffle a FUNCTION TRIM, with LEADING or TRAILING as fits purpose, through a pic 9. TRAILING in this case, or both in the example below.

    identification division.
    program-id. rjust.
    
    data division.
    working-storage section.
    01 str    pic x(10) value '123       '.
    01 some-n pic 9(10).
    
    procedure division.
    
    move function trim(str) to some-n
    move some-n to str
    
    display some-n, " : ", str end-display
    goback.
    
    0000000123 : 0000000123
    

    As Bill mentioned above with validation, this assumes all spaces is the equivalent of 0. That may or may not be a sane thing to allow. Non digits being an issue as well.

提交回复
热议问题