Truncation
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 7 of 23.
When a number does not fit its receiving field, COBOL cuts it without a warning, on both sides of the point: high digits on the left and extra decimals on the right.
01 AMOUNT PIC 9(3)V99.
MOVE 1234.567 TO AMOUNT. *> 234.56Extra decimals are dropped, not rounded: 0.999 moved into a PIC 9V99 field is 0.99, never 1.00.
Arithmetic cuts its result the same way. A 7% tax on 19.99 is 1.3993, and a field with two decimals keeps 1.39:
COMPUTE TAX = PRICE * 0.07. *> 00139Older programs kept years in two digits to save space. Moving 1999 into a PIC 99 field keeps 99, and 2000 becomes 00, which sorts before 99. Finding and widening such fields was the year 2000 problem.
Challenge
EasyTAX is PIC 99V99, too small for the 7% tax on a big price: 1500 prints Tax: 005.00 instead of Tax: 105.00. Change its picture so every price up to 9999.99 keeps its whole tax. Extra decimals still drop, so 19.99 prints Tax: 001.39.
Example input, one line per ACCEPT: 19.99.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. TAXES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 PRICE PIC 9(4)V99.
01 TAX PIC 99V99.
01 TAX-OUT PIC 9(3).99.
PROCEDURE DIVISION.
ACCEPT PRICE.
COMPUTE TAX = PRICE * 0.07.
MOVE TAX TO TAX-OUT.
DISPLAY "Tax: " TAX-OUT.
STOP RUN.