Welcome back to today's session on "COBOL Rounded Clause" or "COBOL Rounding". In this session, you'll learn the basics of the COBOL ROUNDED statement and why you use COBOL rounding in your application programs. You'll go through the COBOL rounding up an example for better understanding. Let's get started with the COBOL Rounded Phrase introduction.
Agenda.
- Introduction.
- What is COBOL ROUNDED Clause (i.e. COBOL ROUNDED Statement)?
- COBOL ROUNDED Syntax.
- COBOL ROUNDED Example.
- ROUNDING in COBOL Example.
- ROUNDED in COBOL Rules.
- Conclusion.
Introduction.
The capability of rounding up or rounding down is another important feature of any programming language. There are many business scenarios where monetary transaction value is either round-up or rounded down based on the requirement. COBOL ROUNDED Phrase (i.e. COBOL ROUNDED statement) is used to implement rounding in COBOL programs. In COBOL, you do not have any default rounding. You've to specify the rounding in your program.
COBOL Rounding Rule of Thumb.
- 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).
What is COBOL ROUNDED Clause (i.e. COBOL ROUNDED Statement)?
COBOL ROUNDED CLAUSE |
Once the decimal point is aligned, the number of places given for the fraction of the resultant identifier is compared to the number of places in the fraction of the product of an arithmetic operation.
Unless the COBOL ROUNDED phrase is known, truncation occurs when the size of the fractional result exceeds the number of places given for its storage.
When the COBOL ROUNDED verb is used, the resulting identifier's least significant digit is increased by 1 if the excess's most significant digit is greater than or equal to 5.
COBOL Rounding or truncation occurs relative to the rightmost integer position for which storage is allocated when the resultant identifier is defined by a PICTURE clause containing the rightmost Ps and the number of places in the measured result exceeds the number of integer positions stated.
Important: The COBOL ROUNDED phrase has no effect in a floating-point arithmetic operation; the output of a floating-point operation is always rounded.
COBOL ROUNDED Syntax.
The syntax of ROUNDED in COBOL is pretty simple and straightforward. You need to specify the ROUNDED keyword as illustrated in the following figure.
ROUNDED in COBOL Example. |
When the result is truncated on either the left or right side after decimal point alignment, this is known as a size error message. Let's look at the following COBOL round down and COBOL rounds up examples.
ROUNDED in COBOL Example.
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
Important Point: Division by 0 always leads to SIZE ERROR.
ROUNDED in COBOL Example:
COMPUTE SALES-TAX ROUNDED = SALES-AMOUNT * .0785
Rounded in COBOL Rules:
- ROUNDED in COBOL can be used with any arithmetic operation.
- Data will be truncated if you did not specify COBOL Rounded Clause and the resultant fields is not sufficient to store the decimal data.
- When the COBOL ROUNDED phrase is used, the output will be rounded to the PICTURE specification of the destination field.
- When ROUNDED in COBOL and REMAINDER are used in the same operation then DIVIDE, ROUNDED must be defined first.
You can code the Rounded clause whenever the result of
a calculation can have more decimal places than is specified in the picture of
the result field. If you don’t use the Rounded clause, the extra decimal places
are truncated
Conclusion.
Finally, this marks an end to our today's COBOL ROUNDED clause. In this session, you learn what is COBOL rounding or COBOL ROUNDING Function. You learned COBOL ROUNDED Statement syntax along with COBOL round-up and COBOL round-down examples for better understanding.
Have a look at CICS Top 100 Interview Questions Part II.
►Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on -
► Youtube
► Facebook
► Linkedin
► Reddit
Thank you for your support.
Mainframe Forum™
No comments:
Post a Comment