MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Output Formatting

Output formatting in COBOL transforms raw data into readable, professional presentations suitable for reports, user interfaces, and data display. COBOL provides powerful formatting capabilities through editing characters in PICTURE clauses, which automatically format numbers with currency symbols, commas, zero suppression, and other enhancements. Understanding output formatting is essential for creating professional reports and user-friendly displays in mainframe applications.

This comprehensive guide will teach you how to use editing characters to format numeric output, create formatted reports, align columns, format dates and times, handle special cases like negative numbers and zero suppression, and follow best practices for professional output formatting. Whether you're creating financial reports, data listings, or user interfaces, proper formatting makes your output clear, readable, and professional.

Understanding Output Formatting

Output formatting in COBOL involves presenting data in a way that's easy to read and understand. This includes:

  • Numeric Formatting: Adding currency symbols, thousands separators, decimal points, and handling negative numbers.
  • Text Alignment: Aligning columns, padding fields, and creating consistent layouts.
  • Date/Time Formatting: Converting dates from internal formats (like YYYYMMDD) to readable formats (like MM/DD/YYYY).
  • Report Layout: Creating headers, detail lines, totals, and properly formatted report structures.

COBOL provides two main mechanisms for output formatting:

  • Editing Characters: Special characters in PICTURE clauses that automatically format data when moved to edited fields.
  • String Building: Using STRING statements to construct formatted output manually.

Editing Characters

Editing characters are special characters used in PICTURE clauses that control how numeric data is displayed. When you MOVE data to a field with editing characters, the editing characters are automatically inserted, creating formatted output.

Common Editing Characters

Here are the most commonly used editing characters:

  • $ (Dollar Sign): Displays currency symbol. Can be floating ($ZZ,ZZ9.99) or fixed position ($,$$$,$$9.99).
  • , (Comma): Inserts thousands separators automatically.
  • Z (Zero Suppression): Replaces leading zeros with spaces. Makes output cleaner.
  • - (Minus/Hyphen): Displays minus sign for negative numbers or used as a separator.
  • / (Slash): Used for date separators (MM/DD/YYYY).
  • . (Period): Decimal point for numeric display.
  • + (Plus): Shows plus sign for positive numbers, minus for negative.
  • CR (Credit): Appears after negative numbers in financial formatting.
  • DB (Debit): Similar to CR, appears after negative numbers.
  • * (Asterisk): Check protection - fills leading positions with asterisks.

Basic Editing Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WORKING-STORAGE SECTION. 01 RAW-AMOUNT PIC 9(6)V99 VALUE 123456.78. 01 FORMATTED-AMOUNT PIC $,$$$,$$9.99. PROCEDURE DIVISION. MAIN-LOGIC. *> Move raw amount to formatted field MOVE RAW-AMOUNT TO FORMATTED-AMOUNT DISPLAY 'Raw: ' RAW-AMOUNT DISPLAY 'Formatted: ' FORMATTED-AMOUNT *> Output: *> Raw: 12345678 *> Formatted: $123,456.78 STOP RUN.

Important: Always use a separate edited field. Don't try to edit the source field directly, as editing characters replace numeric positions.

Numeric Formatting

Numeric formatting is one of the most common formatting tasks. Let's explore various numeric formatting techniques:

Currency Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
WORKING-STORAGE SECTION. 01 AMOUNT PIC 9(7)V99 VALUE 1234567.89. 01 CURRENCY-FORMAT PIC $,$$$,$$9.99. 01 CURRENCY-Z-SUPPR PIC $ZZ,ZZZ,ZZ9.99. 01 SMALL-AMOUNT PIC 9(4)V99 VALUE 12.34. PROCEDURE DIVISION. MAIN-LOGIC. *> Standard currency format MOVE AMOUNT TO CURRENCY-FORMAT DISPLAY 'Currency: ' CURRENCY-FORMAT *> Displays: $1,234,567.89 *> Currency with zero suppression MOVE AMOUNT TO CURRENCY-Z-SUPPR DISPLAY 'With Z: ' CURRENCY-Z-SUPPR *> Displays: $1,234,567.89 *> Small amount with zero suppression MOVE SMALL-AMOUNT TO CURRENCY-Z-SUPPR DISPLAY 'Small: ' CURRENCY-Z-SUPPR *> Displays: $ 12.34 (spaces instead of leading zeros) STOP RUN.

Comma Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WORKING-STORAGE SECTION. 01 LARGE-NUMBER PIC 9(9) VALUE 123456789. 01 COMMA-FORMAT PIC Z,ZZZ,ZZ9. 01 COMMA-DECIMAL PIC Z,ZZZ,ZZ9.99. PROCEDURE DIVISION. MAIN-LOGIC. MOVE LARGE-NUMBER TO COMMA-FORMAT DISPLAY 'With commas: ' COMMA-FORMAT *> Displays: 123,456,789 MOVE 1234.56 TO COMMA-DECIMAL DISPLAY 'Decimal: ' COMMA-DECIMAL *> Displays: 1,234.56 STOP RUN.

Zero Suppression

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WORKING-STORAGE SECTION. 01 NUMBER-1 PIC 9(5) VALUE 00123. 01 NUMBER-2 PIC 9(5) VALUE 00005. 01 Z-SUPPRESSED PIC ZZZZ9. PROCEDURE DIVISION. MAIN-LOGIC. MOVE NUMBER-1 TO Z-SUPPRESSED DISPLAY 'Number 1: [' Z-SUPPRESSED ']' *> Displays: [ 123] (leading zeros become spaces) MOVE NUMBER-2 TO Z-SUPPRESSED DISPLAY 'Number 2: [' Z-SUPPRESSED ']' *> Displays: [ 5] (leading zeros become spaces) STOP RUN.

Negative Number Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
WORKING-STORAGE SECTION. 01 POSITIVE-AMT PIC S9(7)V99 VALUE 1234567.89. 01 NEGATIVE-AMT PIC S9(7)V99 VALUE -1234567.89. 01 MINUS-FORMAT PIC -,$$$,$$9.99. 01 CR-FORMAT PIC $,$$$,$$9.99CR. 01 PLUS-FORMAT PIC +,$$$,$$9.99. PROCEDURE DIVISION. MAIN-LOGIC. *> Minus sign format MOVE NEGATIVE-AMT TO MINUS-FORMAT DISPLAY 'Minus: ' MINUS-FORMAT *> Displays: -$1,234,567.89 *> Credit format (financial) MOVE NEGATIVE-AMT TO CR-FORMAT DISPLAY 'Credit: ' CR-FORMAT *> Displays: $1,234,567.89CR *> Plus/minus format MOVE POSITIVE-AMT TO PLUS-FORMAT DISPLAY 'Plus: ' PLUS-FORMAT *> Displays: +$1,234,567.89 MOVE NEGATIVE-AMT TO PLUS-FORMAT DISPLAY 'Minus: ' PLUS-FORMAT *> Displays: -$1,234,567.89 STOP RUN.

Date Formatting

Dates are often stored in internal formats (like YYYYMMDD) but need to be displayed in readable formats (like MM/DD/YYYY). Here's how to format dates:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
WORKING-STORAGE SECTION. 01 INTERNAL-DATE PIC 9(8) VALUE 20241225. 01 FORMATTED-DATE PIC 99/99/9999. 01 DATE-PARTS. 05 YEAR-PART PIC 9(4). 05 MONTH-PART PIC 9(2). 05 DAY-PART PIC 9(2). 01 DATE-STRING PIC X(10). PROCEDURE DIVISION. MAIN-LOGIC. *> Method 1: Using edited field MOVE INTERNAL-DATE TO FORMATTED-DATE DISPLAY 'Formatted: ' FORMATTED-DATE *> Displays: 12/25/2024 *> Method 2: Using STRING for custom format UNSTRING INTERNAL-DATE INTO YEAR-PART MONTH-PART DAY-PART STRING MONTH-PART '/' DELIMITED BY SIZE DAY-PART '/' DELIMITED BY SIZE YEAR-PART DELIMITED BY SIZE INTO DATE-STRING END-STRING DISPLAY 'Custom: ' DATE-STRING *> Displays: 12/25/2024 STOP RUN.

Report Formatting

Creating well-formatted reports involves headers, aligned columns, detail lines, and totals. Here's a comprehensive example:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
IDENTIFICATION DIVISION. PROGRAM-ID. FORMATTED-REPORT. DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-DATA. 05 CUSTOMER-ID PIC 9(8) VALUE 12345678. 05 CUSTOMER-NAME PIC X(30) VALUE 'JOHN SMITH'. 05 ACCOUNT-BALANCE PIC S9(9)V99 VALUE 1234567.89. 05 TRANSACTION-DATE PIC 9(8) VALUE 20241225. 01 FORMATTED-FIELDS. 05 BALANCE-EDITED PIC $,$$$,$$9.99. 05 DATE-EDITED PIC 99/99/9999. 01 REPORT-LINE PIC X(132). 01 LINE-POSITION PIC 9(3) VALUE 1. PROCEDURE DIVISION. MAIN-LOGIC. PERFORM PRINT-REPORT-HEADER PERFORM PRINT-DETAIL-LINE PERFORM PRINT-TOTALS STOP RUN. PRINT-REPORT-HEADER. DISPLAY ' ' DISPLAY ' CUSTOMER ACCOUNT REPORT' DISPLAY ' ========================' DISPLAY ' ' DISPLAY 'Customer ID Customer Name ' 'Balance Date' DISPLAY '---------- -------------------------- ' '------------ ----------' DISPLAY ' '. PRINT-DETAIL-LINE. *> Format the balance MOVE ACCOUNT-BALANCE TO BALANCE-EDITED *> Format the date MOVE TRANSACTION-DATE TO DATE-EDITED *> Build the detail line MOVE SPACES TO REPORT-LINE MOVE 1 TO LINE-POSITION STRING CUSTOMER-ID DELIMITED BY SIZE ' ' DELIMITED BY SIZE CUSTOMER-NAME DELIMITED BY SIZE ' ' DELIMITED BY SIZE BALANCE-EDITED DELIMITED BY SIZE ' ' DELIMITED BY SIZE DATE-EDITED DELIMITED BY SIZE INTO REPORT-LINE WITH POINTER LINE-POSITION END-STRING DISPLAY REPORT-LINE. PRINT-TOTALS. DISPLAY ' ' DISPLAY '==========================================' DISPLAY 'Total Balance: ' BALANCE-EDITED.

Using STRING for Complex Formatting

When editing characters aren't sufficient, use STRING to build formatted output:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
WORKING-STORAGE SECTION. 01 CUSTOMER-INFO. 05 CUST-ID PIC 9(8) VALUE 12345678. 05 CUST-NAME PIC X(30) VALUE 'JOHN SMITH'. 05 CUST-BALANCE PIC S9(9)V99 VALUE 1234567.89. 01 BALANCE-EDITED PIC $,$$$,$$9.99. 01 FORMATTED-MESSAGE PIC X(100). PROCEDURE DIVISION. MAIN-LOGIC. *> Format the balance first MOVE CUST-BALANCE TO BALANCE-EDITED *> Build formatted message STRING 'Customer: ' DELIMITED BY SIZE CUST-ID DELIMITED BY SIZE ' | Name: ' DELIMITED BY SIZE CUST-NAME DELIMITED BY SIZE ' | Balance: ' DELIMITED BY SIZE BALANCE-EDITED DELIMITED BY SIZE INTO FORMATTED-MESSAGE END-STRING DISPLAY FORMATTED-MESSAGE *> Displays: Customer: 12345678 | Name: JOHN SMITH | Balance: $1,234,567.89 STOP RUN.

Best Practices

Follow these best practices for professional output formatting:

  • Always use separate edited fields: Don't try to edit source fields directly. Define edited fields and MOVE to them.
  • Use meaningful names: Name edited fields clearly (e.g., AMOUNT-EDITED, DATE-FORMATTED) to indicate their purpose.
  • Test with various values: Test formatting with zeros, negatives, large numbers, and edge cases.
  • Maintain consistency: Use the same formatting patterns throughout your reports.
  • Align columns properly: Use fixed positions or STRING with POINTER for consistent alignment.
  • Handle special cases: Consider zero suppression, negative numbers, and missing data.
  • Document formatting requirements: Comment your formatting logic so others understand the requirements.

Summary

Output formatting in COBOL transforms raw data into readable, professional presentations. Editing characters in PICTURE clauses automatically format numbers with currency symbols, commas, zero suppression, and other enhancements. Use separate edited fields for formatted output, test with various values, maintain consistency, and follow best practices for professional results. Proper formatting makes your output clear, readable, and suitable for reports and user interfaces.

Test Your Knowledge

1. What editing character is used for currency formatting?

  • C
  • $
  • M
  • D

2. What does the Z editing character do?

  • Adds zeros
  • Suppresses leading zeros (replaces with spaces)
  • Adds commas
  • Formats dates

3. How do you format a date from YYYYMMDD to MM/DD/YYYY?

  • Use MOVE directly
  • Define an edited field with slashes and MOVE to it
  • Use STRING to build the formatted date
  • Both B and C are correct

4. What is the difference between DISPLAY and WRITE?

  • There is no difference
  • DISPLAY goes to console, WRITE goes to a file
  • DISPLAY is for files, WRITE is for console
  • DISPLAY is faster

5. What editing characters show negative numbers with "CR"?

  • -
  • CR
  • +
  • DB

6. How do you create comma-separated thousands in numeric output?

  • Use the , editing character
  • Use STRING to add commas
  • Use Z editing character
  • Commas are automatic

7. What should you do when formatting output?

  • Edit the source field directly
  • Use a separate edited field and MOVE to it
  • Use DISPLAY only
  • Avoid formatting

8. What is check protection in COBOL formatting?

  • Password protection
  • Using * characters to protect against alteration
  • Error checking
  • Data validation

Related Pages