BudiBadu Logo
Samplebadu

COBOL by Example: Editing Masks

COBOL 2002

Formatting numeric output with PIC editing characters for presentation, using Z for zero suppression to replace leading zeros with spaces, implementing check protection with asterisk fill, displaying signs with plus and minus, and separating data storage from display formatting.

Code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MASKS.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  RAW-NUM     PIC 9(5)V99 VALUE 01234.50.
       01  MASK-Z      PIC ZZZZZ.99.
       01  MASK-STAR   PIC *****.99.
       01  MASK-PLUS   PIC +++++.99.
       
       PROCEDURE DIVISION.
           MOVE RAW-NUM TO MASK-Z.
           DISPLAY "Zero Suppress: " MASK-Z.
           
           MOVE RAW-NUM TO MASK-STAR.
           DISPLAY "Check Protect: " MASK-STAR.
           
           MOVE -123.45 TO MASK-PLUS.
           DISPLAY "Signed:        " MASK-PLUS.
           STOP RUN.

Explanation

Editing masks are used to present data in a human-readable format. They are defined in the PIC clause of the destination variable. When you move a numeric value into an edited field, COBOL automatically formats it according to the mask.

Common editing characters include Z for zero suppression (replacing leading zeros with spaces), * for check protection (filling leading zeros with asterisks), and + or - for sign control. You can also insert static characters like commas, decimals, and currency symbols.

This feature is heavily used in report generation and UI display. It separates the raw data storage (which should be pure numeric) from its presentation, allowing for easy localization and formatting changes without altering the underlying logic.

  • Z: Zero suppression. Replaces leading zeros with spaces.
  • *: Check protection. Replaces leading zeros with asterisks.
  • + / -: Sign control. Displays a plus or minus sign.

Code Breakdown

6
PIC ZZZZZ.99. If the value is 01234.50, this mask prints " 1234.50" (leading zero becomes space).
7
PIC *****.99. If the value is 1234.50, this prints "*1234.50". If the value was 5.00, it would print "****5.00".
8
PIC +++++.99. Displays the sign. If positive, it shows a plus; if negative, a minus.
12
MOVE RAW-NUM TO MASK-Z. The move operation triggers the formatting.