DIVIDE Rounded
DIVIDE Rounded Example
Scenario - Divide one variable with other and result into another variable with ROUNDED option.
Code -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. DIVROUND.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-VAR1 PIC 9(03)V9(2) VALUE 53.12.
05 WS-VAR2 PIC 9(03)V9(2) VALUE 5.5.
05 WS-RESULT1 PIC 9(03).9(2).
05 WS-RESULT2 PIC 9(03).
PROCEDURE DIVISION.
DIVIDE WS-VAR1 BY WS-VAR2 GIVING WS-RESULT1.
DIVIDE WS-VAR1 BY WS-VAR2 GIVING WS-RESULT2 ROUNDED.
DISPLAY "Result (Without Rounding): " WS-RESULT1.
DISPLAY "Result (With Rounding) : " WS-RESULT2.
STOP RUN.
Output -
Result (Without Rounding): 009.65 Result (With Rounding) : 010
Explaining Example -
In the above example:
- It declares four variables: WS-VAR1 and WS-VAR2, both capable of holding decimal value, and initialized with the values 53.12 and 5.5 respectively.
- WS-RESULT1 is declared to hold a decimal value, while WS-RESULT2 is declared to hold a numeric value with 3 digits before the decimal point.
- It divides WS-VAR1 by WS-VAR2 and stores the result in WS-RESULT1. Then, it divides WS-VAR1 by WS-VAR2, rounds the result, and stores it in WS-RESULT2.
- Finally, it displays both results: "Result (Without Rounding): " followed by the content of WS-RESULT1 and "Result (With Rounding): " followed by the content of WS-RESULT2.