Zero Suppression
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 9 of 23.
An edited picture formats a number for people to read. A program moves a number into it and displays it. An edited field holds characters, not a number, so arithmetic cannot use its value.
Z is a digit that prints as a space when it is a leading zero:
01 COUNT-OUT PIC ZZZ9.
MOVE 42 TO COUNT-OUT. *> " 42"End the picture with 9 so zero prints as 0. A picture made only of Z prints zero as all spaces.
Z works with a point too: ZZ9.99 prints 0.5 as 0.50. The digits after the point always print.
An edited field keeps its width, so the leading spaces line numbers up on the right, the way a column of figures should be.
Challenge
EasyDeclare ITEMS-OUT, an edited field of 4 digits, so ITEMS prints without its leading zeros: 42 prints Items: 42 and zero prints Items: 0.
Example input, one line per ACCEPT: 42.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. ITEMCNT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ITEMS PIC 9(4).
*> Declare ITEMS-OUT here
PROCEDURE DIVISION.
ACCEPT ITEMS.
MOVE ITEMS TO ITEMS-OUT.
DISPLAY "Items: " ITEMS-OUT.
STOP RUN.