Menu
CoddyTech

ROUNDED

Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 16 of 23.

Arithmetic drops the digits that do not fit. ROUNDED rounds the last digit kept instead:

COMPUTE TAX ROUNDED = PRICE * 0.07.
*> 19.99 * 0.07 is 1.3993: 1.40

ROUNDED goes right after the receiving field, in every arithmetic verb:

ADD A B GIVING TOTAL ROUNDED.
DIVIDE 3 INTO SHARE-AMT ROUNDED.

A half rounds away from zero: 2.345 rounded to two decimals is 2.35, and -2.345 is -2.35.

Money is usually rounded once, at the end. Rounding at each step and adding the results can end a cent away from rounding the total.

challenge icon

Challenge

Easy

This program prints a 7% tax but drops the extra digits, so 19.99 prints Tax:   1.39. Make the COMPUTE round, so it prints Tax:   1.40.

Example input, one line per ACCEPT: 19.99.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. RTAX.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 PRICE PIC 9(4)V99.
       01 TAX PIC 9(3)V99.
       01 TAX-OUT PIC ZZ9.99.
       PROCEDURE DIVISION.
           ACCEPT PRICE.
           COMPUTE TAX = PRICE * 0.07.
           MOVE TAX TO TAX-OUT.
           DISPLAY "Tax: " TAX-OUT.
           STOP RUN.

All lessons in Data and Numbers