Edited numeric output
An amount on a report is not the amount in the file. The file has digits and a sign; the report has a currency sign, thousands separators and a point. COBOL writes that difference into the picture, with a set of editing characters that turn a number into a printable field.
Z hides a leading zero
A 9 in a picture always prints a digit, zeros included. A Z prints a space when the position holds a leading zero, which is what a report wants:
IDENTIFICATION DIVISION.
PROGRAM-ID. EDIT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 PLAIN-F PIC S9(5)V99 VALUE 1234.5.
01 ED-ZF PIC Z(5).99 VALUE 1234.5.
01 ED-CF PIC $ZZ,ZZ9.99 VALUE 1234.5.
01 ED-CR PIC 9(5).99CR VALUE 1234.5.
01 ED-DB PIC 9(5).99DB VALUE 1234.5.
01 ED-PL PIC +9(5).99 VALUE 1234.5.
01 ED-MI PIC 9(5).99- VALUE 1234.5.
01 ZERO-F PIC Z(5).99 VALUE 0.
PROCEDURE DIVISION.
DISPLAY "plain [" PLAIN-F "]".
DISPLAY "Z [" ED-ZF "]".
DISPLAY "cur [" ED-CF "]".
DISPLAY "CR [" ED-CR "]".
DISPLAY "DB [" ED-DB "]".
DISPLAY "plus [" ED-PL "]".
DISPLAY "minus [" ED-MI "]".
DISPLAY "zero [" ZERO-F "]".
STOP RUN.
cobc -x -free -Wall -o edit edit.cob
./edit
plain [+01234.50]
Z [ 1234.50]
cur [$ 1,234.50]
CR [01234.50 ]
DB [01234.50 ]
plus [+01234.50]
minus [01234.50 ]
zero [ .00]
The plain field prints every digit, the Z field one space where the zero was, and a field of zeros prints as a point with nothing in front of it. The zero line is the one that surprises: Z(5).99 with a value of zero is five spaces, the point and two zeros, so a report column of zeros becomes a column of dots unless the program prints a dash for zero.
Currency, commas and the point
$, , and . are also characters of the picture, and each one takes a position in the field. A currency sign written before a Z floats: it sits next to the leftmost significant digit, so $ 1,234.50 and $12,345.00 both look right in their own width.
The price of the edit is length. The plain field is eight bytes, the currency field is twelve, and the four extra bytes are the sign, the space, the comma and the dollar. An edited field is for output and for a file another system reads as text; it is not for arithmetic, which is the subject of the fourth chapter.
CR, DB and a trailing sign
A positive amount has no sign to print, and the sign characters leave two blanks where the CR would go. A negative amount fills them:
IDENTIFICATION DIVISION.
PROGRAM-ID. NEG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ED-CR PIC 9(5).99CR VALUE 0.
01 ED-DB PIC 9(5).99DB VALUE 0.
01 ED-MI PIC 9(5).99- VALUE 0.
01 ED-PL PIC +9(5).99 VALUE 0.
PROCEDURE DIVISION.
MOVE -1234.5 TO ED-CR.
MOVE -1234.5 TO ED-DB.
MOVE -1234.5 TO ED-MI.
MOVE -1234.5 TO ED-PL.
DISPLAY "CR [" ED-CR "]".
DISPLAY "DB [" ED-DB "]".
DISPLAY "minus [" ED-MI "]".
DISPLAY "plus [" ED-PL "]".
STOP RUN.
cobc -x -free -Wall -o neg neg.cob
./neg
CR [01234.50CR]
DB [01234.50DB]
minus [01234.50-]
plus [-01234.50]
CR and DB are the accounting abbreviations for credit and debit, and they are two characters because that is how a ledger prints a negative. The trailing minus and the leading plus are one character each, and either can be put on the other end of the picture. The choice is the style of the report, and the picture is where the style lives.
An edited field is text
A picture with an editing character does not describe a number, it describes a string that looks like one. Numeric input to it is converted and edited, and arithmetic on it is refused:
IDENTIFICATION DIVISION.
PROGRAM-ID. ADD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ED-ZF PIC Z(5).99 VALUE 1234.5.
PROCEDURE DIVISION.
ADD 1 TO ED-ZF.
DISPLAY ED-ZF.
STOP RUN.
cobc -x -free -Wall -o add add.cob
add.cob:9: error: 'ED-ZF' is not a numeric name
7 |
8 | PROCEDURE DIVISION.
9 > ADD 1 TO ED-ZF.
10 | DISPLAY ED-ZF.
11 | STOP RUN.
The program keeps the amount in a plain numeric field and edits it on the way out, with a MOVE into the edited field. That is the pattern of every report: compute in decimal, print in edited.
What the picture refuses
An edited picture carries its own sign, so an S next to the editing characters is a contradiction and the compiler says which one it found first:
IDENTIFICATION DIVISION.
PROGRAM-ID. RULES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ED-CR PIC S9(5).99CR VALUE 0.
PROCEDURE DIVISION.
DISPLAY ED-CR.
STOP RUN.
cobc -x -free -Wall -o rules rules.cob
rules.cob:6: error: . cannot follow S
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 ED-CR PIC S9(5).99CR VALUE 0.
7 |
8 | PROCEDURE DIVISION.
The compiler reports the point first and would have reported CR or DB cannot follow S next; both mean the same thing. The picture without the S, as in the second chapter, accepts a negative value and prints the sign with the characters the picture chose.
Exercises
- Give an amount of 0.50 the picture Z(5).99 and print it. Where does the space end and the digits begin?
- Write a picture that prints a negative amount in parentheses, as an accountant would. Which editing characters does it need, and what happens to a positive one?
- Move 1234567.89 into a $ZZ,ZZZ,ZZ9.99 field and print it. Count the bytes of the field and explain each one.
- Print the same amount as 9(7).99 and as Z(7).99 and put the two lines side by side. Which column would a reader use to compare magnitudes?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a Z in a numeric picture print?
Answer
A space where the position holds a leading zero
The position is still there and still takes a byte; Z prints a space instead of a leading zero, which is what a report column needs.
-
What does a positive amount print in a field with CR at the end?
Answer
The digits and two spaces where CR would be
The two character positions exist in the field, and a positive amount leaves them blank.
-
What can be done with an edited numeric field?
Answer
It can receive a numeric value and be printed, but not do arithmetic
The editing characters make it a printable string; the compiler refuses it as the receiver of ADD or as an arithmetic operand.
-
How many bytes does
PIC $ZZ,ZZ9.99take for a value of 1234.5?Answer
Eleven: the dollar, five edited digits, the comma, three digits, the point and two decimals
Every character of an edited picture is a position: $, four Z positions and a 9, a comma, two more 9 positions, the point and two decimals.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 ZERO-F PIC Z(5).99 VALUE 0. PROCEDURE DIVISION. DISPLAY "[" ZERO-F "]". STOP RUN.Answer
[ .00]
Five Z positions print five spaces for the zero integer part, and the point with its two decimals follows.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 ED-CF PIC $ZZ,ZZ9.99 VALUE 1234.5. PROCEDURE DIVISION. DISPLAY "[" ED-CF "]". STOP RUN.Answer
[$ 1,234.50]
The currency sign floats next to the first significant digit, and the comma comes from the picture position after the thousands.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 ED-CR PIC 9(5).99CR VALUE 0. PROCEDURE DIVISION. MOVE -1234.5 TO ED-CR. DISPLAY "[" ED-CR "]". STOP RUN.Answer
[01234.50CR]
A negative value fills the two character positions of CR, and the digits keep their full width.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 ED-MI PIC 9(5).99- VALUE 0. PROCEDURE DIVISION. MOVE -1234.5 TO ED-MI. DISPLAY "[" ED-MI "]". STOP RUN.Answer
[01234.50-]
The minus is the last position of the picture, so it prints after the digits.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 ED-PL PIC +9(5).99 VALUE 0. PROCEDURE DIVISION. MOVE -1234.5 TO ED-PL. DISPLAY "[" ED-PL "]". STOP RUN.Answer
[-01234.50]
The plus position prints a minus for a negative value and a plus for a positive one.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 ED-CR PIC S9(5).99CR VALUE 0. PROCEDURE DIVISION. DISPLAY ED-CR. STOP RUN.Answer
error: . cannot follow S
The compiler reads the picture left to right, so the point after the S is the first contradiction it reports.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev