Subtract Rounded
Subtract Rounded Example
Scenario - Subtract one decimal value from other and placing result into third rounded.
Code -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. SUBTROUN.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-A PIC 9(03)V9(02) VALUE 20.12.
05 WS-B PIC 9(03)V9(02) VALUE 30.67.
05 WS-C PIC 9(03).9(02).
05 WS-D PIC 9(03).
PROCEDURE DIVISION.
SUBTRACT WS-A FROM WS-B GIVING WS-C.
SUBTRACT WS-A FROM WS-B GIVING WS-D ROUNDED.
DISPLAY "WS-C (WITHOUT ROUNDED): " WS-C.
DISPLAY "WS-D (WITH ROUNDED) : " WS-D.
STOP RUN.
Output -
WS-C (WITHOUT ROUNDED): 010.55 WS-D (WITH ROUNDED) : 011
Explaining Example -
In the above example:
- It defines numeric variables WS-A, WS-B, WS-C, and WS-D.
- WS-A and WS-B are defined as packed-decimal variables with two decimal places, initialized to 20.12 and 30.67, respectively.
- WS-C is defined as a floating-point variable with two decimal places, and WS-D is defined as an integer variable.
- It subtracts WS-A from WS-B, storing the result in WS-C. Another subtraction is performed with WS-A from WS-B, but this time the rounded result is stored in WS-D.
- Finally, it displays both results, WS-C without rounding and WS-D with rounding.