Numeric formatting in COBOL is how you make numbers look right on reports and screens: with commas, dollar signs, no leading zeros, or a sign. You use an edited PICTURE (with characters like Z, $, and ,) on a field and MOVE a numeric value into it. The result is for display only; you do not use that field in arithmetic. For the full set of PICTURE characters see PICTURE clause.
| Character | Meaning |
|---|---|
| Z | Suppress leading zero; replace with space |
| $ | Currency symbol; fixed or floating |
| , | Thousands separator (comma) |
| . | Decimal point in display |
| + - | Plus or minus sign in output |
Keep calculations in numeric fields (PIC 9, S9, V, etc.). When you need to print or display, MOVE the value to an edited field. The edited field has Z, $, comma, or sign in its PICTURE and is used only as a destination for MOVE (or in WRITE/DISPLAY), not in ADD, SUBTRACT, or COMPUTE.
12345678910WORKING-STORAGE SECTION. 01 WS-AMOUNT PIC S9(7)V99. *> For arithmetic 01 WS-OUT PIC $Z,ZZZ,ZZ9.99. *> For display PROCEDURE DIVISION. COMPUTE WS-AMOUNT = 1234.56 * 2 MOVE WS-AMOUNT TO WS-OUT DISPLAY WS-OUT *> Shows something like $ 1,234.56 (format depends on compiler) STOP RUN.
Z in a PICTURE means “replace a leading zero with a space.” So PIC ZZZ9 has room for up to 4 digits; 0 displays as " 0", 42 as " 42", 1234 as "1234". Use Z in the leading positions and 9 for the rightmost digit so at least one digit (or zero) always shows.
Comma (,) in the PICTURE inserts a comma in the output (e.g. thousands separator). Period (.) is the decimal point. Example: PIC Z,ZZZ,ZZ9.99 gives you spaces for leading zeros, commas, and two decimal places. The exact placement and “blank when zero” rules depend on the compiler.
The computer stores the number in a “math” box. When it’s time to show it to a person, you copy it into a “display” box that has rules: “put a dollar sign here, use commas, and don’t show leading zeros.” The display box is only for showing; you don’t do math in it.
1. What does the Z editing character do?
2. Should you use an edited field (with $, Z, comma) in COMPUTE or ADD?