MainframeMaster

COBOL Tutorial

COBOL ROUNDED Clause - Quick Reference

Progress0 of 0 lessons

Overview

The ROUNDED clause is used in arithmetic operations to control decimal precision. It ensures that decimal results are properly rounded according to standard rounding rules rather than being truncated.

Purpose and Usage

  • Decimal precision - Control rounding behavior in arithmetic
  • Financial accuracy - Ensure precise financial calculations
  • Standard rounding - Apply standard rounding rules
  • Precision control - Avoid truncation errors
  • Data integrity - Maintain calculation accuracy

Rounding Concept

Standard Rounding Rules:
3.1 → 3 (rounds down)
3.4 → 3 (rounds down)
3.5 → 4 (rounds up)
3.9 → 4 (rounds up)
ROUNDED applies these rules automatically

ROUNDED follows standard mathematical rounding rules.

Syntax

The ROUNDED clause can be used with all basic arithmetic operations.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
* Basic ROUNDED syntax with arithmetic operations ADD source TO target ROUNDED SUBTRACT source FROM target ROUNDED MULTIPLY source BY target ROUNDED DIVIDE source INTO target ROUNDED DIVIDE source BY divisor GIVING result ROUNDED * With COMPUTE statement COMPUTE result = expression ROUNDED * Complete example IDENTIFICATION DIVISION. PROGRAM-ID. ROUNDED-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMOUNT-1 PIC 9(5)V99 VALUE 123456. 01 AMOUNT-2 PIC 9(5)V99 VALUE 789012. 01 RESULT PIC 9(6)V99. 01 INTEREST-RATE PIC 9(3)V9999 VALUE 12345. 01 PRINCIPAL PIC 9(8)V99 VALUE 10000000. 01 INTEREST PIC 9(8)V99. PROCEDURE DIVISION. MAIN-LOGIC. * Addition with ROUNDED ADD AMOUNT-1 TO AMOUNT-2 GIVING RESULT ROUNDED DISPLAY "Rounded addition: " RESULT * Division with ROUNDED DIVIDE PRINCIPAL BY 12 GIVING RESULT ROUNDED DISPLAY "Rounded division: " RESULT * Interest calculation with ROUNDED MULTIPLY PRINCIPAL BY INTEREST-RATE GIVING INTEREST ROUNDED DIVIDE INTEREST BY 10000 GIVING INTEREST ROUNDED DISPLAY "Rounded interest: " INTEREST STOP RUN.

ROUNDED can be used with all arithmetic operations and COMPUTE.

ROUNDED vs Truncation Comparison

OperationWith ROUNDEDWithout ROUNDED
10 ÷ 3 (2 decimals)3.333.32
15.5 + 2.518.018.0
100 × 0.0757.57.5
25.7 ÷ 38.578.56

Practical Examples

These examples demonstrate how to use the ROUNDED clause effectively in different calculation scenarios.

Financial Calculations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
IDENTIFICATION DIVISION. PROGRAM-ID. FINANCIAL-CALCULATIONS. DATA DIVISION. WORKING-STORAGE SECTION. 01 PRINCIPAL PIC 9(8)V99 VALUE 10000000. 01 INTEREST-RATE PIC 9(3)V9999 VALUE 8750. 01 YEARS PIC 9(2) VALUE 5. 01 INTEREST PIC 9(8)V99. 01 MONTHLY-PAYMENT PIC 9(6)V99. 01 TOTAL-AMOUNT PIC 9(8)V99. PROCEDURE DIVISION. CALCULATE-LOAN. * Calculate annual interest MULTIPLY PRINCIPAL BY INTEREST-RATE GIVING INTEREST ROUNDED DIVIDE INTEREST BY 10000 GIVING INTEREST ROUNDED * Calculate total amount with compound interest COMPUTE TOTAL-AMOUNT = PRINCIPAL * (1 + INTEREST-RATE / 10000) ** YEARS ROUNDED * Calculate monthly payment DIVIDE TOTAL-AMOUNT BY YEARS GIVING MONTHLY-PAYMENT ROUNDED DIVIDE MONTHLY-PAYMENT BY 12 GIVING MONTHLY-PAYMENT ROUNDED DISPLAY "Principal: " PRINCIPAL DISPLAY "Interest Rate: " INTEREST-RATE DISPLAY "Annual Interest: " INTEREST DISPLAY "Total Amount: " TOTAL-AMOUNT DISPLAY "Monthly Payment: " MONTHLY-PAYMENT STOP RUN.

ROUNDED ensures precise financial calculations.

Tax Calculations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
* Tax calculation with ROUNDED DATA DIVISION. WORKING-STORAGE SECTION. 01 GROSS-INCOME PIC 9(8)V99 VALUE 7500000. 01 TAX-RATE PIC 9(3)V9999 VALUE 2500. 01 DEDUCTIONS PIC 9(6)V99 VALUE 125000. 01 TAXABLE-INCOME PIC 9(8)V99. 01 TAX-AMOUNT PIC 9(7)V99. 01 NET-INCOME PIC 9(8)V99. PROCEDURE DIVISION. CALCULATE-TAX. * Calculate taxable income SUBTRACT DEDUCTIONS FROM GROSS-INCOME GIVING TAXABLE-INCOME ROUNDED * Calculate tax amount MULTIPLY TAXABLE-INCOME BY TAX-RATE GIVING TAX-AMOUNT ROUNDED DIVIDE TAX-AMOUNT BY 10000 GIVING TAX-AMOUNT ROUNDED * Calculate net income SUBTRACT TAX-AMOUNT FROM GROSS-INCOME GIVING NET-INCOME ROUNDED DISPLAY "Gross Income: " GROSS-INCOME DISPLAY "Deductions: " DEDUCTIONS DISPLAY "Taxable Income: " TAXABLE-INCOME DISPLAY "Tax Rate: " TAX-RATE DISPLAY "Tax Amount: " TAX-AMOUNT DISPLAY "Net Income: " NET-INCOME.

ROUNDED ensures accurate tax calculations.

Percentage Calculations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
* Percentage calculations with ROUNDED DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-SALES PIC 9(8)V99 VALUE 12500000. 01 PRODUCT-SALES PIC 9(6)V99 VALUE 325000. 01 PERCENTAGE PIC 9(3)V9999. 01 DISCOUNT-RATE PIC 9(3)V9999 VALUE 1500. 01 DISCOUNT-AMOUNT PIC 9(6)V99. PROCEDURE DIVISION. CALCULATE-PERCENTAGES. * Calculate percentage of total sales MULTIPLY PRODUCT-SALES BY 100 GIVING PERCENTAGE ROUNDED DIVIDE PERCENTAGE BY TOTAL-SALES GIVING PERCENTAGE ROUNDED * Calculate discount amount MULTIPLY PRODUCT-SALES BY DISCOUNT-RATE GIVING DISCOUNT-AMOUNT ROUNDED DIVIDE DISCOUNT-AMOUNT BY 10000 GIVING DISCOUNT-AMOUNT ROUNDED DISPLAY "Total Sales: " TOTAL-SALES DISPLAY "Product Sales: " PRODUCT-SALES DISPLAY "Percentage: " PERCENTAGE DISPLAY "Discount Rate: " DISCOUNT-RATE DISPLAY "Discount Amount: " DISCOUNT-AMOUNT.

ROUNDED ensures precise percentage calculations.

Inventory Calculations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
* Inventory calculations with ROUNDED DATA DIVISION. WORKING-STORAGE SECTION. 01 UNITS-SOLD PIC 9(6) VALUE 1250. 01 UNIT-PRICE PIC 9(5)V99 VALUE 2999. 01 TOTAL-REVENUE PIC 9(8)V99. 01 COST-PER-UNIT PIC 9(5)V99 VALUE 1899. 01 TOTAL-COST PIC 9(8)V99. 01 PROFIT-MARGIN PIC 9(3)V9999. 01 PROFIT-AMOUNT PIC 9(7)V99. PROCEDURE DIVISION. CALCULATE-INVENTORY. * Calculate total revenue MULTIPLY UNITS-SOLD BY UNIT-PRICE GIVING TOTAL-REVENUE ROUNDED * Calculate total cost MULTIPLY UNITS-SOLD BY COST-PER-UNIT GIVING TOTAL-COST ROUNDED * Calculate profit amount SUBTRACT TOTAL-COST FROM TOTAL-REVENUE GIVING PROFIT-AMOUNT ROUNDED * Calculate profit margin percentage MULTIPLY PROFIT-AMOUNT BY 100 GIVING PROFIT-MARGIN ROUNDED DIVIDE PROFIT-MARGIN BY TOTAL-REVENUE GIVING PROFIT-MARGIN ROUNDED DISPLAY "Units Sold: " UNITS-SOLD DISPLAY "Unit Price: " UNIT-PRICE DISPLAY "Total Revenue: " TOTAL-REVENUE DISPLAY "Cost Per Unit: " COST-PER-UNIT DISPLAY "Total Cost: " TOTAL-COST DISPLAY "Profit Amount: " PROFIT-AMOUNT DISPLAY "Profit Margin: " PROFIT-MARGIN.

ROUNDED ensures accurate inventory calculations.

Best Practices and Considerations

Understanding best practices ensures accurate and reliable calculations.

Best Practices

  • Use consistently - Apply ROUNDED to all financial calculations
  • Check field sizes - Ensure target fields can accommodate rounded results
  • Understand business rules - Verify rounding behavior meets requirements
  • Test thoroughly - Verify calculations with known test cases
  • Document usage - Clearly specify when ROUNDED is used
  • Consider precision - Balance precision needs with performance

Common Use Cases

Use CaseDescriptionExample
Financial CalculationsInterest, taxes, paymentsInterest = Principal × Rate ROUNDED
Percentage CalculationsRatios, discounts, marginsPercentage = (Part / Total) × 100 ROUNDED
Inventory ManagementCosts, profits, quantitiesTotal = Units × Price ROUNDED
Statistical CalculationsAverages, ratios, ratesAverage = Sum / Count ROUNDED
Currency ConversionsExchange rates, conversionsConverted = Amount × Rate ROUNDED

Performance Considerations

  • Minimal impact - ROUNDED has negligible performance effect
  • Arithmetic operation - Only affects calculation time
  • Precision vs speed - ROUNDED improves accuracy
  • Field size impact - Larger fields may affect performance
  • Consistent usage - Use ROUNDED consistently for best results

ROUNDED Clause Quick Reference

OperationSyntaxExample
ADDADD source TO target ROUNDEDADD AMOUNT TO TOTAL ROUNDED
SUBTRACTSUBTRACT source FROM target ROUNDEDSUBTRACT DEDUCTION FROM GROSS ROUNDED
MULTIPLYMULTIPLY source BY target ROUNDEDMULTIPLY RATE BY AMOUNT ROUNDED
DIVIDEDIVIDE source INTO target ROUNDEDDIVIDE TOTAL INTO SHARES ROUNDED
COMPUTECOMPUTE result = expression ROUNDEDCOMPUTE RESULT = A + B * C ROUNDED

Test Your Knowledge

1. What is the primary purpose of the ROUNDED clause in COBOL?

  • To round decimal numbers to the nearest whole number
  • To control decimal precision in arithmetic operations
  • To truncate decimal numbers
  • To format numbers for display

2. In which arithmetic operations can ROUNDED be used?

  • Only ADD operations
  • Only MULTIPLY operations
  • ADD, SUBTRACT, MULTIPLY, and DIVIDE operations
  • Only COMPUTE operations

3. What happens when ROUNDED is not specified in an arithmetic operation?

  • The result is automatically rounded
  • The result is truncated (decimal places are dropped)
  • An error occurs
  • The operation fails

4. How does ROUNDED handle the value 3.5?

  • It rounds down to 3
  • It rounds up to 4
  • It truncates to 3
  • It depends on the field definition

5. Can ROUNDED be used with the COMPUTE statement?

  • No, only with individual arithmetic verbs
  • Yes, with the ROUNDED phrase
  • Only with simple COMPUTE statements
  • Only with complex expressions

Frequently Asked Questions