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.
ROUNDED follows standard mathematical rounding rules.
The ROUNDED clause can be used with all basic arithmetic operations.
123456789101112131415161718192021222324252627282930313233343536373839* 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.
Operation | With ROUNDED | Without ROUNDED |
---|---|---|
10 ÷ 3 (2 decimals) | 3.33 | 3.32 |
15.5 + 2.5 | 18.0 | 18.0 |
100 × 0.075 | 7.5 | 7.5 |
25.7 ÷ 3 | 8.57 | 8.56 |
These examples demonstrate how to use the ROUNDED clause effectively in different calculation scenarios.
1234567891011121314151617181920212223242526272829303132IDENTIFICATION 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.
12345678910111213141516171819202122232425262728* 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.
123456789101112131415161718192021222324* 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.
123456789101112131415161718192021222324252627282930313233* 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.
Understanding best practices ensures accurate and reliable calculations.
Use Case | Description | Example |
---|---|---|
Financial Calculations | Interest, taxes, payments | Interest = Principal × Rate ROUNDED |
Percentage Calculations | Ratios, discounts, margins | Percentage = (Part / Total) × 100 ROUNDED |
Inventory Management | Costs, profits, quantities | Total = Units × Price ROUNDED |
Statistical Calculations | Averages, ratios, rates | Average = Sum / Count ROUNDED |
Currency Conversions | Exchange rates, conversions | Converted = Amount × Rate ROUNDED |
Operation | Syntax | Example |
---|---|---|
ADD | ADD source TO target ROUNDED | ADD AMOUNT TO TOTAL ROUNDED |
SUBTRACT | SUBTRACT source FROM target ROUNDED | SUBTRACT DEDUCTION FROM GROSS ROUNDED |
MULTIPLY | MULTIPLY source BY target ROUNDED | MULTIPLY RATE BY AMOUNT ROUNDED |
DIVIDE | DIVIDE source INTO target ROUNDED | DIVIDE TOTAL INTO SHARES ROUNDED |
COMPUTE | COMPUTE result = expression ROUNDED | COMPUTE RESULT = A + B * C ROUNDED |
1. What is the primary purpose of the ROUNDED clause in COBOL?
2. In which arithmetic operations can ROUNDED be used?
3. What happens when ROUNDED is not specified in an arithmetic operation?
4. How does ROUNDED handle the value 3.5?
5. Can ROUNDED be used with the COMPUTE statement?
Understanding basic arithmetic operations in COBOL.
Using COMPUTE for complex arithmetic expressions.
Understanding numeric data types in COBOL.
Financial calculation techniques in COBOL.
Controlling precision in COBOL calculations.