Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Debugging
Debugging | Beginner | COBOL

Fix Decimal Alignment

Problem Description

Result shows wrong value:
01 WS-PRICE PIC 99V99 VALUE 12.50.
01 WS-QTY PIC 99 VALUE 5.
01 WS-TOTAL PIC 99V99.
MULTIPLY WS-PRICE BY WS-QTY GIVING WS-TOTAL.

Expected Output

Should show 62.50, not truncated value

Hints

Check if result field can hold the computed value.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. FIXDECIMAL. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-PRICE PIC 99V99 VALUE 12.50. 01 WS-QTY PIC 99 VALUE 5. * FIX: Result field too small. 99V99 max is 99.99 * 12.50 * 5 = 62.50, but 99.99 * 99 could overflow 01 WS-TOTAL PIC 9(4)V99. 01 WS-DISPLAY PIC Z,ZZ9.99. PROCEDURE DIVISION. MULTIPLY WS-PRICE BY WS-QTY GIVING WS-TOTAL. MOVE WS-TOTAL TO WS-DISPLAY. DISPLAY "TOTAL: " WS-DISPLAY. STOP RUN.

Explanation:

Result field must be large enough. Use edited PIC for display.