MainframeMaster

COBOL Tutorial

COBOL FORMAT Clause - Quick Reference

Progress0 of 0 lessons

Overview

The FORMAT clause is used to specify formatting rules for data display and output operations in COBOL. This clause controls how data is presented, including field alignment, padding, width, and other display characteristics to ensure consistent and professional-looking output.

Purpose and Usage

  • Data presentation - Control how data is displayed
  • Field alignment - Specify left, right, or center alignment
  • Padding control - Define padding characters and behavior
  • Width specification - Control field display width
  • Consistent formatting - Ensure uniform data presentation

Formatting Concept

Format Flow: [Raw Data] → [FORMAT Rules] → [Formatted Output] → [Display]
Format Types: [Alignment] [Padding] [Width] [Decimal] [Currency] [Date]
Display Control: [Field Width] [Alignment] [Padding] [Spacing] [Symbols]
FORMAT provides controlled and consistent data presentation

FORMAT enables professional and consistent data presentation in COBOL programs.

Syntax

The FORMAT clause follows specific syntax patterns and is used within data description entries and display operations.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* Basic FORMAT clause syntax 01 data-item PIC X(20) FORMAT IS format-specification. * Examples 01 CUSTOMER-NAME PIC X(30) FORMAT IS "LEFT-JUSTIFIED". 01 ACCOUNT-BALANCE PIC 9(8)V99 FORMAT IS "RIGHT-JUSTIFIED". 01 REPORT-DATE PIC X(10) FORMAT IS "CENTERED". 01 CURRENCY-AMOUNT PIC 9(8)V99 FORMAT IS "CURRENCY". * Complete example with multiple formats DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-HEADER. 05 COMPANY-NAME PIC X(40) FORMAT IS "CENTERED". 05 REPORT-TITLE PIC X(30) FORMAT IS "CENTERED". 05 REPORT-DATE PIC X(10) FORMAT IS "RIGHT-JUSTIFIED". 01 CUSTOMER-DATA. 05 CUSTOMER-ID PIC 9(5) FORMAT IS "LEFT-JUSTIFIED". 05 CUSTOMER-NAME PIC X(30) FORMAT IS "LEFT-JUSTIFIED". 05 ACCOUNT-BALANCE PIC 9(8)V99 FORMAT IS "CURRENCY". 05 LAST-TRANSACTION PIC X(10) FORMAT IS "CENTERED".

FORMAT is used in data description entries to specify display formatting.

Format Types

Format TypeDescriptionUse Case
LEFT-JUSTIFIEDAlign data to the leftText fields and labels
RIGHT-JUSTIFIEDAlign data to the rightNumeric fields and amounts
CENTEREDCenter data in fieldHeaders and titles
CURRENCYFormat as currencyMonetary amounts
DATEFormat as dateDate fields

Usage in Display Operations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
* Using FORMAT in display operations PROCEDURE DIVISION. * Display formatted data DISPLAY "Customer Report" FORMAT IS "CENTERED" DISPLAY "=================" FORMAT IS "CENTERED" * Display customer information with formatting DISPLAY "ID: " CUSTOMER-ID FORMAT IS "LEFT-JUSTIFIED" DISPLAY "Name: " CUSTOMER-NAME FORMAT IS "LEFT-JUSTIFIED" DISPLAY "Balance: " ACCOUNT-BALANCE FORMAT IS "CURRENCY" DISPLAY "Date: " LAST-TRANSACTION FORMAT IS "CENTERED" * Display formatted report line DISPLAY CUSTOMER-ID " " CUSTOMER-NAME " " ACCOUNT-BALANCE " " LAST-TRANSACTION FORMAT IS "LEFT-JUSTIFIED, LEFT-JUSTIFIED, CURRENCY, CENTERED" * Display with custom formatting DISPLAY "Total: " TOTAL-AMOUNT FORMAT IS "RIGHT-JUSTIFIED, CURRENCY"

FORMAT is used in DISPLAY statements to control output formatting.

Common Use Cases

FORMAT is commonly used in specific scenarios where controlled data presentation is needed.

Report Generation

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
* Report generation with FORMAT DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-HEADER. 05 COMPANY-NAME PIC X(40) FORMAT IS "CENTERED". 05 REPORT-TITLE PIC X(30) FORMAT IS "CENTERED". 05 REPORT-DATE PIC X(10) FORMAT IS "RIGHT-JUSTIFIED". 01 REPORT-LINE. 05 PRODUCT-ID PIC 9(5) FORMAT IS "LEFT-JUSTIFIED". 05 PRODUCT-NAME PIC X(20) FORMAT IS "LEFT-JUSTIFIED". 05 QUANTITY PIC 9(4) FORMAT IS "RIGHT-JUSTIFIED". 05 UNIT-PRICE PIC 9(6)V99 FORMAT IS "CURRENCY". 05 TOTAL-PRICE PIC 9(8)V99 FORMAT IS "CURRENCY". PROCEDURE DIVISION. MAIN-LOGIC. * Display report header DISPLAY COMPANY-NAME FORMAT IS "CENTERED" DISPLAY REPORT-TITLE FORMAT IS "CENTERED" DISPLAY "Date: " REPORT-DATE FORMAT IS "RIGHT-JUSTIFIED" DISPLAY "==========================================" * Display column headers DISPLAY "ID Product Name Qty Unit Price Total" DISPLAY "---- -------------------- ---- ----------- --------" * Display report data PERFORM DISPLAY-REPORT-LINE UNTIL END-OF-FILE = 'Y' * Display totals DISPLAY "==========================================" DISPLAY "Total: " GRAND-TOTAL FORMAT IS "RIGHT-JUSTIFIED, CURRENCY" STOP RUN. DISPLAY-REPORT-LINE. DISPLAY PRODUCT-ID " " PRODUCT-NAME " " QUANTITY " " UNIT-PRICE " " TOTAL-PRICE FORMAT IS "LEFT-JUSTIFIED, LEFT-JUSTIFIED, RIGHT-JUSTIFIED, CURRENCY, CURRENCY" READ PRODUCT-FILE AT END MOVE 'Y' TO END-OF-FILE END-READ.

Use FORMAT for professional report generation with consistent formatting.

Data Display and 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
28
29
30
31
32
33
34
35
36
37
38
39
40
* Data display with FORMAT DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-DISPLAY. 05 CUSTOMER-ID PIC 9(5) FORMAT IS "LEFT-JUSTIFIED". 05 CUSTOMER-NAME PIC X(30) FORMAT IS "LEFT-JUSTIFIED". 05 ACCOUNT-BALANCE PIC 9(8)V99 FORMAT IS "CURRENCY". 05 LAST-TRANSACTION PIC X(10) FORMAT IS "CENTERED". 01 TRANSACTION-DISPLAY. 05 TRANSACTION-ID PIC 9(6) FORMAT IS "LEFT-JUSTIFIED". 05 TRANSACTION-DATE PIC X(10) FORMAT IS "CENTERED". 05 TRANSACTION-AMOUNT PIC 9(8)V99 FORMAT IS "CURRENCY". 05 TRANSACTION-TYPE PIC X(10) FORMAT IS "LEFT-JUSTIFIED". PROCEDURE DIVISION. MAIN-LOGIC. * Display customer information DISPLAY "Customer Information" DISPLAY "===================" DISPLAY "ID: " CUSTOMER-ID FORMAT IS "LEFT-JUSTIFIED" DISPLAY "Name: " CUSTOMER-NAME FORMAT IS "LEFT-JUSTIFIED" DISPLAY "Balance: " ACCOUNT-BALANCE FORMAT IS "CURRENCY" DISPLAY "Last Transaction: " LAST-TRANSACTION FORMAT IS "CENTERED" * Display transaction history DISPLAY "Transaction History" DISPLAY "===================" PERFORM DISPLAY-TRANSACTION UNTIL END-OF-TRANSACTIONS = 'Y' STOP RUN. DISPLAY-TRANSACTION. DISPLAY TRANSACTION-ID " " TRANSACTION-DATE " " TRANSACTION-AMOUNT " " TRANSACTION-TYPE FORMAT IS "LEFT-JUSTIFIED, CENTERED, CURRENCY, LEFT-JUSTIFIED" READ TRANSACTION-FILE AT END MOVE 'Y' TO END-OF-TRANSACTIONS END-READ.

Use FORMAT for consistent data display and user-friendly output.

Formatted Input/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
28
29
30
31
32
33
* Formatted input/output with FORMAT DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT-FORMAT. 05 INPUT-PROMPT PIC X(20) FORMAT IS "LEFT-JUSTIFIED". 05 INPUT-FIELD PIC X(30) FORMAT IS "LEFT-JUSTIFIED". 01 OUTPUT-FORMAT. 05 OUTPUT-LABEL PIC X(15) FORMAT IS "RIGHT-JUSTIFIED". 05 OUTPUT-VALUE PIC X(20) FORMAT IS "LEFT-JUSTIFIED". PROCEDURE DIVISION. MAIN-LOGIC. * Display formatted prompts DISPLAY "Enter customer name: " FORMAT IS "LEFT-JUSTIFIED" ACCEPT CUSTOMER-NAME DISPLAY "Enter account number: " FORMAT IS "LEFT-JUSTIFIED" ACCEPT ACCOUNT-NUMBER * Display formatted results DISPLAY "Customer: " CUSTOMER-NAME FORMAT IS "LEFT-JUSTIFIED" DISPLAY "Account: " ACCOUNT-NUMBER FORMAT IS "LEFT-JUSTIFIED" DISPLAY "Balance: " ACCOUNT-BALANCE FORMAT IS "CURRENCY" * Display formatted menu DISPLAY "Select option:" FORMAT IS "CENTERED" DISPLAY "1. View Balance" FORMAT IS "LEFT-JUSTIFIED" DISPLAY "2. Make Deposit" FORMAT IS "LEFT-JUSTIFIED" DISPLAY "3. Make Withdrawal" FORMAT IS "LEFT-JUSTIFIED" DISPLAY "4. Exit" FORMAT IS "LEFT-JUSTIFIED" STOP RUN.

Use FORMAT for professional input/output interfaces.

Best Practices and Tips

Following these best practices ensures effective use of the FORMAT clause for proper data presentation.

FORMAT Design Principles

  • Consistent formatting - Use consistent formatting throughout your application
  • Appropriate alignment - Use left alignment for text, right for numbers
  • Clear visual hierarchy - Use formatting to create visual structure
  • User-friendly presentation - Make output easy to read and understand
  • Professional appearance - Ensure output looks professional
  • Performance consideration - Consider performance impact of complex formatting

Common Pitfalls to Avoid

PitfallProblemSolution
Inconsistent formattingConfusing and unprofessional outputUse consistent formatting patterns
Over-formattingCluttered and hard to read outputKeep formatting simple and clean
Inappropriate alignmentPoor readability and visual appealUse appropriate alignment for data types
Ignoring field widthData overflow and display issuesConsider field width in formatting
Performance neglectSlow output with complex formattingBalance formatting with performance

Performance Considerations

  • Formatting overhead - Formatting operations add processing time
  • Complex format specifications - Complex formats increase overhead
  • Large data volumes - Formatting large amounts of data can be slow
  • Display frequency - Frequent formatting operations impact performance
  • Memory usage - Formatting may require additional memory
  • Compiler optimization - Some compilers optimize formatting operations

When to Use FORMAT

Use CaseFORMAT SuitabilityReasoning
Report generationExcellentPerfect for professional report formatting
Data displayExcellentIdeal for user-friendly data presentation
User interfacesGoodGood for formatted user interfaces
Performance-critical outputPoorFormatting overhead may be too high
Simple data outputPoorOverkill for simple output needs

FORMAT Clause Quick Reference

UsageSyntaxExample
Basic formatFORMAT IS format-typeFORMAT IS "LEFT-JUSTIFIED"
Left alignmentFORMAT IS "LEFT-JUSTIFIED"FORMAT IS "LEFT-JUSTIFIED"
Right alignmentFORMAT IS "RIGHT-JUSTIFIED"FORMAT IS "RIGHT-JUSTIFIED"
Center alignmentFORMAT IS "CENTERED"FORMAT IS "CENTERED"
Currency formatFORMAT IS "CURRENCY"FORMAT IS "CURRENCY"

Test Your Knowledge

1. What is the primary purpose of the FORMAT clause in COBOL?

  • To define data types
  • To specify formatting rules for data display and output
  • To control file operations
  • To perform calculations

2. Where is the FORMAT clause typically used?

  • In DATA DIVISION
  • In data description entries and display operations
  • In PROCEDURE DIVISION
  • In ENVIRONMENT DIVISION

3. What is the main benefit of using FORMAT?

  • Faster execution
  • Controlled data presentation and consistent output formatting
  • Memory optimization
  • Simpler syntax

4. When is the FORMAT clause most useful?

  • In programs without output
  • In programs requiring formatted data display and reports
  • Only during compilation
  • Only during program termination

5. How does FORMAT relate to data display?

  • They are completely independent
  • FORMAT controls how data is presented and displayed to users
  • FORMAT overrides display features
  • They serve the same purpose

Frequently Asked Questions