COBOL COMMA
The COMMA symbol in COBOL is used for numeric formatting as a thousands separator, making large numbers more readable in reports, displays, and business documents. It's an essential element of professional data presentation in financial and business applications.
Overview and Purpose
The comma in COBOL serves as a formatting character in picture clauses to separate thousands groups in numeric displays. This formatting convention makes large numbers significantly more readable and is standard practice in business applications. When you display a number like 1234567, it's much easier to read as 1,234,567 with proper comma formatting. COBOL automatically handles the placement and suppression of commas based on the actual value being displayed.
Basic Comma Formatting
123401 DISPLAY-AMOUNT PIC Z,ZZZ,ZZ9.99. MOVE 1234567.89 TO DISPLAY-AMOUNT DISPLAY "Amount: " DISPLAY-AMOUNT
This example demonstrates basic comma formatting for a monetary amount. The picture clause Z,ZZZ,ZZ9.99 provides positions for up to 7 digits with comma separators every three positions. When the value 1234567.89 is moved to this field, it displays as "1,234,567.89". The Z characters provide zero suppression for leading zeros, while the commas automatically appear in the correct positions for thousands separation.
Comma Suppression with Small Numbers
1234501 SMALL-NUMBER PIC Z,ZZZ,ZZ9. MOVE 123 TO SMALL-NUMBER DISPLAY "Small: " SMALL-NUMBER *> Displays: "Small: 123"
When the number is smaller than the comma positions, COBOL automatically suppresses the commas and replaces them with spaces (when using Z for zero suppression). In this example, the number 123 displays as " 123" without any commas because there aren't enough digits to require thousands separation. This automatic suppression ensures that small numbers don't have unnecessary punctuation.
Currency Symbol with Comma Formatting
1234501 CURRENCY-AMOUNT PIC $Z,ZZZ,ZZ9.99. MOVE 45678.50 TO CURRENCY-AMOUNT DISPLAY "Price: " CURRENCY-AMOUNT *> Displays: "Price: $45,678.50"
Combining currency symbols with comma formatting creates professional financial displays. The dollar sign appears at the beginning of the formatted number, followed by the properly comma-separated digits. This format is standard for financial reports, invoices, and any business document where monetary amounts need to be clearly presented. The formatting automatically adjusts based on the number size.
Tutorial: Implementing Professional Number Formatting
Step-by-Step Tutorial
Step 1: Define Formatted Fields
123401 FORMATTED-NUMBERS. 05 SALES-AMOUNT PIC $*,***,**9.99. 05 QUANTITY-FIELD PIC Z,ZZZ,ZZ9. 05 PERCENTAGE-FIELD PIC ZZ9.99%.
Start by defining various formatted fields for different types of numeric data. Use appropriate picture clauses that include comma formatting where needed.
Step 2: Move Data to Formatted Fields
123MOVE WS-TOTAL-SALES TO SALES-AMOUNT MOVE WS-ITEM-COUNT TO QUANTITY-FIELD MOVE WS-GROWTH-RATE TO PERCENTAGE-FIELD
Move your computational data to the formatted display fields. COBOL automatically applies the formatting defined in the picture clauses.
Step 3: Display Formatted Results
123DISPLAY "Total Sales: " SALES-AMOUNT DISPLAY "Items Sold: " QUANTITY-FIELD DISPLAY "Growth Rate: " PERCENTAGE-FIELD
Display the formatted fields to present professional-looking numeric data with proper comma separation and other formatting elements.
Practical Exercises
Practice Exercises
Exercise 1: Financial Report Formatting
Create picture clauses for a financial report that displays revenue, expenses, and profit with proper comma formatting and currency symbols.
Show Solution
123401 FINANCIAL-DISPLAY. 05 REVENUE-OUT PIC $Z,ZZZ,ZZZ,ZZ9.99. 05 EXPENSES-OUT PIC $Z,ZZZ,ZZZ,ZZ9.99. 05 PROFIT-OUT PIC +$Z,ZZZ,ZZZ,ZZ9.99.
Exercise 2: Inventory Count Display
Design formatting for inventory counts that can range from single items to millions, using appropriate comma placement and zero suppression.
Show Solution
123401 INVENTORY-DISPLAY. 05 ITEM-COUNT PIC Z,ZZZ,ZZ9. 05 TOTAL-VALUE PIC $Z,ZZZ,ZZZ.99. 05 AVERAGE-COST PIC $ZZZ.99.
Exercise 3: Check Printing Format
Create a picture clause for check printing that uses asterisk fill with comma formatting to prevent alteration of the amount.
Show Solution
12301 CHECK-AMOUNT PIC $*,***,**9.99. *> This format fills leading positions with asterisks *> Example: $***1,234.56 for amount 1234.56
Advanced Comma Formatting Techniques
International Number Formatting
1234SPECIAL-NAMES. DECIMAL-POINT IS COMMA. 01 EUROPEAN-FORMAT PIC Z.ZZZ.ZZ9,99.
For international applications, you can reverse the roles of commas and periods using the DECIMAL-POINT IS COMMA clause. This changes the formatting to use periods as thousands separators and commas as decimal separators, which is standard in many European countries.
Conditional Formatting
1234567IF WS-AMOUNT > 999999 MOVE WS-AMOUNT TO LARGE-FORMAT DISPLAY "Amount: " LARGE-FORMAT ELSE MOVE WS-AMOUNT TO SMALL-FORMAT DISPLAY "Amount: " SMALL-FORMAT END-IF
Implement conditional formatting to use different picture clauses based on the size of the number. This allows for optimal display formatting that adapts to the data range while maintaining professional appearance.
Test Your Knowledge
Question 1: Comma Placement
In the picture clause PIC Z,ZZZ,ZZ9, where will commas appear for the number 12345?
Show Answer
A) 12,345 - The comma appears between the thousands and hundreds positions, following standard thousands separation rules.
Question 2: Small Number Display
How would the number 42 display in a field with PIC Z,ZZZ,ZZ9?
Show Answer
C) ___42 (spaces where commas would be) - With Z editing, commas are suppressed and replaced with spaces when not needed for thousands separation.
Question 3: International Formatting
What does the DECIMAL-POINT IS COMMA clause affect?
Show Answer
C) Both decimal points and thousands separators are swapped - This clause reverses the roles of commas and periods for international number formatting.