0

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.
2
  • 2
    PIC 'ZZZZ9' or 'Z(4)9' USAGE DISPLAY will do what you want. Commented Sep 12, 2023 at 10:55
  • consider use of MOVE FUNCTION NUMVAL(ACCPT-NUM) TO USED-NUM ... or even TO ACCPT-NUM. Commented Sep 12, 2023 at 12:00

1 Answer 1

1

No need to do all that you did. Just define your final data item with leading Z to suppress the zeros automatically for you.

Example:

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:

IBM COBOL Data Definitions

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.