ROUNDED

The ROUNDED option used to round the fraction result of the compute statement exceeds the length of the target data item fractional places. After decimal point alignment, the number of fraction places in arithmetic operation is compared with the number of fraction places of result identifier. If the size of the fractional result exceeds the number of factional digits declared in the resultant identifier, truncation occurs and the excess fractional digits are ignored. If the ROUNDED option is specified, the least significant digit of the resultant identifier is increased by 1 if the next fraction value is greater than 5. In a floating-point arithmetic operation, the result of a floating-point operation is always rounded and the ROUNDED phrase has no effect.

  • ROUNDED option can be used with ADD, SUBTRACT, DIVIDE and MULTIPLY verbs
  • ROUNDED option is used to round off the value as per PICTURE clause specification of the receiving field
  • It generally takes effect when after decimal point alignment, the result calculated must be truncated on the right hand side
  • ROUNDED option is usually coded following the field to be rounded. However, when coded with REMAINDER option of DIVIDE verb, it is prefixed
COMPUTE < data-item-1> [ROUNDED] ... = arithmetic-expression 
[ ON SIZE ERROR < imperative statement-1 ] .. 
[ NOT ON SIZE ERROR < imperative-statement-2> ] ..
END-COMPUTE
  • If the ending value is 5 or more then round Up (i.e. COBOL round up).
  • If the ending value is 4 or less then round Down (i.e. COBOL round down).
Destination Field|Actual Result|Truncated Result|Rounded Result
PIC 9(03)v9.      123.26        123.2            123.3
PIC 9(03)v9.      234.54        234.5            234.5 
PIC 9(03).        456.25        456              456 

Example:

05 WS-A PIC 99V999 VALUE 20.534.
05 WS-B PIC 99V999 VALUE 11.963.
05 WS-C PIC 99V9.

ADD DATA-A DATA-B GIVING DATA-C ROUNDED

After execution of the above ADD statement,

       DATA-A will have 20.534

       DATA-B will have 11.963

       DATA-C will have 32.5

In the above case, the actual sum of DATA-A and DATA-B is 32.497 but since the ROUNDED option is used, the result is rounded off and hence DATA-C contains the final value as 32.5. If suppose, the ROUNDED option was not coded, then DATA-C would have a final value of 32.4.

Using ROUNDED option for the REMAINDER option of DIVIDE statement:-
DIVIDE DATA-A INTO DATA-B GIVING DATA-C ROUNDED REMAINDER DATA-D

Note: In the above DIVIDE statement, since ROUNDED is used for REMAINDER, it is prefixed.

This will show the process for converting a packed-numeric format to a display (or zoned-decimal) format. The fewer decimal positions of the result field will be rounded because of the ROUNDED keyword on the ADD statement. The Result field is also defined with a SIGN TRAILING SEPARATE that creates an additional byte to the field for the sign.

The Source Field is defined as a Packed (or COMP-3) field with 6 digits and 3 decimal positions. The following is the WORKING-STORAGE definition for the source field.

01  PACK-DECIMAL-SIGN-6-3   PIC S9(6)V999  COMP-3.

The Result Field is defined as a Zoned Decimal (or USAGE IS DISPLAY) field with 7 digits and 2 decimal positions. The following is the WORKING-STORAGE definition for the result field.

01  DISPLAY-SIGN-7-2        PIC S9(7)V99 SIGN TRAILING SEPARATE.

The following PROCEDURE DIVISION statement shows how to convert from a PACKED to a DISPLAY (with SIGN TRAILING SEPARATE) numeric value.

ADD PACK-DECIMAL-SIGN-6-3 TO ZERO  
    GIVING DISPLAY-SIGN-7-2 ROUNDED

This will show the process for converting a packed-numeric format to an edited numeric format. The fewer decimal positions of the result field will be rounded because of the ROUNDED keyword on the ADD statement. The mask for the Result field will cause an explicit decimal point to be inserted.

The Source Field is defined as a Packed (or COMP-3) field with 6 digits and 3 decimal positions. The following is the WORKING-STORAGE definition for the source field.

01  PACK-DECIMAL-SIGN-6-3   PIC S9(6)V999  COMP-3.

The Result Field is defined as a Zoned Decimal (or USAGE IS DISPLAY) field with 7 digits and 2 decimal positions. The following is the WORKING-STORAGE definition for the result field.

01  EDIT-FIELD-7-2          pic Z,ZZZ,ZZZ.99+.

The following PROCEDURE DIVISION statement shows how to convert from a PACKED to a EDITED numeric value.

ADD PACK-DECIMAL-SIGN-6-3 TO ZERO
    GIVING EDIT-FIELD-7-2 ROUNDED

This will show the process for converting a binary numeric format to a display (or zoned-decimal) format. The fewer decimal positions of the result field will be rounded because of the ROUNDED keyword on the ADD statement. The Result field is also defined with a SIGN TRAILING SEPARATE that creates an additional byte to the field for the sign.

The Source Field is defined as a Binary (or COMP) field with 6 digits and 3 decimal positions. The following is the WORKING-STORAGE definition for the source field.

01  BINARY-SIGN-6-3         PIC S9(6)V999  COMP VALUE 0.

The Result Field is defined as a Zoned Decimal (or USAGE IS DISPLAY) field with 7 digits and 2 decimal positions. The following is the WORKING-STORAGE definition for the result field.

01  DISPLAY-SIGN-7-2        PIC S9(7)V99 SIGN TRAILING SEPARATE.

The following PROCEDURE DIVISION statement shows how to convert from a BINARY to a DISPLAY (with SIGN TRAILING SEPARATE) numeric value.

ADD BINARY-SIGN-6-3 TO ZERO 
    GIVING DISPLAY-SIGN-7-2 ROUNDED       

This test case will show the process for converting a binary numeric format to an edited numeric format. The fewer decimal positions of the result field will be rounded because of the ROUNDED keyword on the ADD statement. The mask for the Result field will cause an explicit decimal point to be inserted.

The Source Field is defined as a Binary (or COMP) field with 6 digits and 3 decimal positions. The following is the WORKING-STORAGE definition for the source field.

01  BINARY-SIGN-6-3         PIC S9(6)V999  COMP VALUE 0.

The Result Field is defined as a Zoned Decimal (or USAGE IS DISPLAY) field with 7 digits and 2 decimal positions. The following is the WORKING-STORAGE definition for the result field.

01  EDIT-FIELD-7-2          pic Z,ZZZ,ZZZ.99+.

The following PROCEDURE DIVISION statement shows how to convert from a BINARY to a DISPLAY (with SIGN TRAILING SEPARATE) numeric value.

ADD BINARY-SIGN-6-3 TO ZERO
    GIVING DISPLAY-SIGN-7-2 ROUNDED        

This test case will show the process for converting a zoned-decimal-numeric format to a display format. The fewer decimal positions of the result field will be rounded because of the ROUNDED keyword on the ADD statement. The Result field is also defined with a SIGN TRAILING SEPARATE that creates an additional byte to the field for the sign.

The Source Field is defined as a Zoned Decimal field with 6 digits and 3 decimal positions. The following is the WORKING-STORAGE definition for the source field.

01  ZONED-DECIMAL-SIGN-6-3   pic S9(6)V999  VALUE 0.

The Result Field is defined as a Display field with 7 digits and 2 decimal positions. The following is the WORKING-STORAGE definition for the result field.

01  DISPLAY-SIGN-7-2        PIC S9(7)V99 SIGN TRAILING SEPARATE.

The following PROCEDURE DIVISION statement shows how to convert from a ZONED-DECIMAL to a DISPLAY (with SIGN TRAILING SEPARATE) numeric value.

ADD ZONED-DECIMAL-SIGN-6-3 TO ZERO 
    GIVING DISPLAY-SIGN-7-2 ROUNDED

This test case will show the process for converting a zoned-decimal-numeric format to an edited numeric format. The fewer decimal positions of the result field will be rounded because of the ROUNDED keyword on the ADD statement. The mask for the Result field will cause an explicit decimal point to be inserted.

The Source Field is defined as a Zoned Decimal field with 6 digits and 3 decimal positions. The following is the WORKING-STORAGE definition for the source field.

01  ZONED-DECIMAL-SIGN-6-3   pic S9(6)V999  VALUE 0.

The Result Field is defined as a Display field with 7 digits and 2 decimal positions. The following is the WORKING-STORAGE definition for the result field.

01  EDIT-FIELD-7-2          pic Z,ZZZ,ZZZ.99+.

The following PROCEDURE DIVISION statement shows how to convert from a ZONED-DECIMAL to an EDITED numeric value.

ADD ZONED-DECIMAL-SIGN-6-3 TO ZERO 
    GIVING EDIT-FIELD-7-2 ROUNDED 

A size error condition can occur in four different ways –

  • When the result value of an arithmetic expression, exceeds the largest value that can be contained in the result field.
  • When division by zero occurs.
  • When the year of the arithmetic statement result falls outside the century window.
  • In an exponential expression, as indicated in the following table
Size errorAction taken when a SIZE ERROR clause is not present
Zero raised to zero powerThe value returned is 1, and a message is issued
Zero raised to a negative numberThe program is terminated abnormally
A negative number raised to a fractional powerThe absolute value of the base is used, and a message is issued

The size error condition applies to results, not to any intermediate results. If the ROUNDED phrase is specified, rounding takes place before size error checking.

When a size error occurs, the subsequent action of the program depends on the action specified in ON SIZE ERROR phrase. If the ON SIZE ERROR phrase is not specified and a size error condition occurs, truncation rules apply and the resultant stores in the identifier.

If the ON SIZE ERROR phrase is specified and a size error condition occurs, the resultant value affected by the size error is not altered; i.e., the error results are not placed in the receiving identifier. After completion of the execution of the arithmetic operation, the imperative statements in the ON SIZE ERROR phrase are executed, control is transferred to the end of the arithmetic statement.

If an individual arithmetic operation causes a size error condition (for ADD CORRESPONDING and SUBTRACT CORRESPONDING statements), the ON SIZE ERROR imperative statement is not executed until all the individual additions or subtractions have been completed.

If the NOT ON SIZE ERROR phrase has been specified and a size error condition does not exist, the NOT ON SIZE ERROR phrase is executed.

ON SIZE ERROR option can be used with the following arithmetic statements.

ADD ... ON SIZE ERROR ...
SUBTRACT ... ON SIZE ERROR ...
MULTIPLY ... ON SIZE ERROR ...
DIVIDE ... ON SIZE ERROR ... 
COMPUTE ... ON SIZE ERROR ...

COBOL Blog: Click Here IBM Reference: Click Here

Scroll to Top