ROUNDED Phrase
ROUNDED Phrase
- ROUNDED phrase is used in arithmetic operations (ADD, SUBTRACT, DIVIDE, MULTIPLY, and COMPUTE) to round the result to the nearest whole number.
- It affects the decimal values of an arithmetic operation result and makes the result a whole number.
Syntax -
ADD source-var TO target-var ROUNDED.
Similarly, SUBTRACT, MULTIPLY and DIVIDE have ROUNDED coded with target-var.
COMPUTE target-var ROUNDED = var1 + var2.
Fractions Validation Process -
- After decimal point alignment, the number of fraction places in arithmetic operation is compared with the number of fraction places (declared) of result variable.
- If the size of fraction digits exceeds the number of factional digits declared in result variable, truncation occurs, and the excess fractional digits gets ignored.
For Example - In below example, If ROUNDED option was not coded, then WS-OP is 27 because of the truncation of fractional digits.
Rounding -
If the ROUNDED is coded,
- The least significant digit (last digit) of the result variable is increased by 1 if the left most fraction value is greater than .5.
- The least significant digit of the result variable has no effect if the left most fraction value is less than .5.
For Example - In below example, the actual sum of WS-INP-A and WS-INP-B is 27.61. But because of the ROUNDED option and faction is greater than .5, the result is 28.
Practical Example -
Scenario - Adding two decimal values and stores into a variable.
Declaration -
----+----1----+----2----+----3----+----4----+----5----+----6
05 WS-INP-A PIC 9(3)V9(2) VALUE 12.83.
05 WS-INP-B PIC 9(2)V9(2) VALUE 14.78.
05 WS-OP PIC 9(2).
In PROCEDURE DIVISION.
----+----1----+----2----+----3----+----4----+----5----+----6
ADD WS-INP-A TO WS-INP-B
GIVING WS-OP ROUNDED.
Result -
WS-INP-A = 12.83
WS-INP-B = 14.78
WS-OP = 28
Explaining -
In the above example, the actual sum of WS-INP-A and WS-INP-B is 27.61. But because of the ROUNDED option, the result is rounded, and WS-OP contains the value 28. If the ROUNDED option is not coded, WS-OP is 27, and the fraction value is ignored.