Implied Decimals
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 6 of 23.
The V in PIC 9(3)V99 is an implied point: the field stores 5 digits, and the picture says the last two are decimals. No byte holds the point.
MOVE lines numbers up on their points, not on their last digits. A value moved into a field with fewer decimals keeps its whole part:
01 PRICE PIC 9(3)V99 VALUE 12.75.
01 SHORT-P PIC 9(3)V9.
MOVE PRICE TO SHORT-P.
*> 0127, which is 12.7Arithmetic lines up the points too, so fields with different decimals add up correctly:
01 FEE PIC 9V9 VALUE 0.5.
ADD FEE TO PRICE. *> 01325DISPLAY prints only the digits, so the picture is the one place that says where the point is: 01325 is 13.25 in a 9(3)V99 field and 132.5 in a 9(4)V9 one.
Challenge
EasyThe program adds two prices into TOTAL, but TOTAL has no decimal places, so 2.50 and 1.75 print 00004. Give it two decimal places and 5 digits in all, so the same prices print 00425.
Example input, one line per ACCEPT: 2.50, 1.75.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. TOTALS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 PRICE-A PIC 9(4)V99.
01 PRICE-B PIC 9(4)V99.
01 TOTAL PIC 9(5) VALUE ZERO.
PROCEDURE DIVISION.
ACCEPT PRICE-A.
ACCEPT PRICE-B.
COMPUTE TOTAL = PRICE-A + PRICE-B.
DISPLAY TOTAL.
STOP RUN.