COBOL Tutorial

Progress0 of 0 lessons

COBOL numeric formatting

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.

Common editing characters

Editing characters for numeric display
CharacterMeaning
ZSuppress leading zero; replace with space
$Currency symbol; fixed or floating
,Thousands separator (comma)
.Decimal point in display
+ -Plus or minus sign in output

Edited vs numeric fields

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.

cobol
1
2
3
4
5
6
7
8
9
10
WORKING-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: suppress leading zeros

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 and decimal point

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.

Step-by-step: formatting a number for display

  • Define a numeric field (PIC 9(n)V9(m) or S9(n)V9(m)) for the value. Do all arithmetic in this field.
  • Define an edited field with the display shape you want (e.g. PIC $Z,ZZZ,ZZ9.99).
  • MOVE the numeric field TO the edited field. Use the edited field in DISPLAY or WRITE, not in calculations.

Explain like I'm five

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.

Test Your Knowledge

1. What does the Z editing character do?

  • Stores zero
  • Suppresses leading zeros and shows spaces instead
  • Marks the end of a number
  • Adds zeros

2. Should you use an edited field (with $, Z, comma) in COMPUTE or ADD?

  • Yes, for currency
  • No; use numeric fields for arithmetic and MOVE to edited for display
  • Only with ROUNDED
  • Yes, if it has a decimal point

Related concepts

Related Pages