Compute Rounded Example


Scenario - Calculating the result of expression and rounding it to nearest integer.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMPUTER.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INP1        PIC 9(3)V9(2) VALUE 240.22.
          05 WS-INP2        PIC 9(2)V9(2) VALUE 10.14.
          05 WS-INP3        PIC 9(2)V9(2) VALUE 20.13.
          05 WS-OP1         PIC 9(5).9(2).
          05 WS-OP2         PIC 9(5).

       PROCEDURE DIVISION.

           COMPUTE WS-OP1   = ( WS-INP1 + WS-INP2 ) * WS-INP3.
           DISPLAY "WS-OP1 (WITHOUT ROUNDING): " WS-OP1.
           COMPUTE WS-OP2 ROUNDED = ( WS-INP1 + WS-INP2 ) * WS-INP3.
           DISPLAY "WS-OP2 (WITH ROUNDING):    " WS-OP2.

           STOP RUN.

Output -

WS-OP1 (WITHOUT ROUNDING): 05039.74
WS-OP2 (WITH ROUNDING):    05040

Explaining Example -

In the above example:

  • It calculates the result of adding WS-INP1 and WS-INP2, then multiplying the sum by WS-INP3, and stores it in WS-OP1 (without any rounding applied).
  • It calculates the result of adding WS-INP1 and WS-INP2, then multiplying the sum by WS-INP3, and stores it in WS-OP2 with rounding applied.
  • Displays both WS-OP1, WS-OP2 and terminates the program execution.