In the Cobol language: I use a numeric variable that is initially defined as the size. If the user typed a number smaller than the size I declared, there are unnecessary zeros at the beginning. How do I delete leading zeros in the variable?
This is what I did: but it didn't work
IDENTIFICATION DIVISION. PROGRAM-ID. REMOVE. AUTHOR. TZS. DATA DIVISION. WORKING-STORAGE SECTION. 01 VN-CONSTANT PIC 9(2) VALUE 5. 01 VN-NUM PIC 9(5). 01 VA-STNUM PIC X(5). 01 VN-NUM-OF-SPACES PIC 9. 01 VA-ENDNUM PIC X(5). 01 VA-RESULT PIC X(4). PROCEDURE DIVISION. DISPLAY 'ENTER A NUMBER UP TO 5 DIGIT:'. ACCEPT VN-NUM. PERFORM CALL-CHECK-NO-SPACES. STOP RUN. CALL-CHECK-NO-SPACES. MOVE VN-NUM TO VA-STNUM. INSPECT VA-STNUM TALLYING VN-NUM-OF-SPACES FOR LEADING SPACES DISPLAY VN-NUM-OF-SPACES IF VN-NUM-OF-SPACES NOT EQUAL TO 0 SUBTRACT VN-NUM-OF-SPACES FROM VN-CONSTANT. COMPUTE VN-NUM-OF-SPACES = VN-NUM-OF-SPACES + 1. MOVE VA-STNUM ( VN-NUM-OF-SPACES : VN-CONSTANT ) TO VA-ENDNUM. DISPLAY '_NO-SPACES__' VA-ENDNUM. DISPLAY 'VN-NUM___' VA-STNUM. DISPLAY '_VA-NUM__' VA-STNUM.
No need to do all that you did. Just define your final data item with leading Z to suppress the zeros automatically for you.
01 VA-ENDNUM PIC ZZZZ9.
or this is also correct:
01 VA-ENDNUM PIC Z(4)9.
The Z suppresses leading zeros. The '9' represents a number that will never be suppressed. (In other words, your data item is actually 5 characters with the 5th one being a numeric value of 0-9.) I hope this helps.
For more information - check out the link below: