COBOL by Example: PIC Clauses
Exploring advanced Picture clause features for signed numbers using S prefix, implementing implied decimal points with V for fixed-point arithmetic without storage overhead, creating display-formatted output with editing characters like dollar signs and commas, and understanding the difference between computational and display picture formats for numeric data processing.
Code
IDENTIFICATION DIVISION.
PROGRAM-ID. PIC-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SINGLE-DIGIT PIC 9.
01 SIGNED-NUM PIC S99 VALUE -15.
01 DECIMAL-NUM PIC 99V99 VALUE 12.34.
01 FORMATTED-NUM PIC $$,$$9.99 VALUE 1234.56.
PROCEDURE DIVISION.
MOVE 5 TO SINGLE-DIGIT.
DISPLAY "Single: " SINGLE-DIGIT.
DISPLAY "Signed: " SIGNED-NUM.
DISPLAY "Decimal (Internal): " DECIMAL-NUM.
DISPLAY "Formatted: " FORMATTED-NUM.
STOP RUN.Explanation
PIC clauses offer powerful features beyond simple types. They handle signed numbers, implied decimals, and display formatting. The S character in a PIC string (e.g., S99) indicates that the number is signed. Without it, all numbers are treated as positive, and any negative sign assigned to it will be lost.
The V character represents an implied decimal point. PIC 99V99 means the variable holds 4 digits, but the system treats it as having two decimal places. For example, the value 12.34 is stored as 1234. The decimal point does not take up any storage space; it is purely logical.
In this code example, we also use editing characters like $, ,, and .. A PIC string like $$,$$9.99 is used for presentation. It automatically inserts currency symbols, commas for thousands separators, and a physical decimal point. This feature makes generating reports in COBOL incredibly efficient.

