codegurukool.com · Free coding tutorials & online compiler

Home › COBOL Tutorial › COBOL ROUNDED and SIZE ERROR

COBOL ROUNDED and SIZE ERROR

Control precision and overflow.

By default extra decimals are cut off. Add ROUNDED to round to the nearest value. If a result is too big for its PIC, ON SIZE ERROR lets you react instead of storing a wrong number. End the statement with END-COMPUTE.

Example code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. RND.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 N PIC 99.
       PROCEDURE DIVISION.
           COMPUTE N = 7 / 2.
           DISPLAY N.
           COMPUTE N ROUNDED = 7 / 2.
           DISPLAY N.
           COMPUTE N = 150 * 2
               ON SIZE ERROR DISPLAY "Too big"
           END-COMPUTE.
           STOP RUN.

Run this code in the online editor

← COBOL arithmeticCOBOL edited pictures →