MainframeMaster

COBOL Tutorial

STANDARD-DECIMAL - Decimal Numbers

Progress0 of 0 lessons

DISPLAY vs PACKED-DECIMAL

DISPLAY is human-readable; PACKED-DECIMAL (COMP-3) is compact exact decimal. Use PACKED-DECIMAL for computation and persistent storage of money; convert to edited DISPLAY for printing.

Declaring Decimals and Rounding

cobol
1
2
3
4
5
01 PRICE PIC 9(5)V99 PACKED-DECIMAL. 01 QUANTITY PIC 9(5) DISPLAY. 01 TOTAL PIC 9(7)V99 PACKED-DECIMAL. ... COMPUTE TOTAL ROUNDED = PRICE * QUANTITY

Use ROUNDED to control rounding during COMPUTE.

Edited Output

cobol
1
2
3
01 TOTAL-EDIT PIC ZZ,ZZZ,ZZ9.99-. MOVE TOTAL TO TOTAL-EDIT DISPLAY TOTAL-EDIT

Edited pictures provide commas, decimal point, and sign formatting for users.

Precision and Options

  • Define enough integer and fractional digits to prevent overflow
  • Be aware of compiler options like ARITH/TRUNC that influence precision

Best Practices and Common Mistakes

Best Practices

  • Use PACKED-DECIMAL for calculations; DISPLAY/edited for presentation
  • Apply ROUNDED where domain requires
  • Unit test boundary values (e.g., 99999.99)

Common Mistakes

MistakeProblemFix
Using BINARY for moneyInexact decimal valuesUse PACKED-DECIMAL
Missing ROUNDEDUnexpected truncationAdd ROUNDED

Quick Reference

FeatureUsageExample
Implied decimalV in PICTURE9(7)V99
Exact decimalPACKED-DECIMALPIC 9(5)V99 COMP-3
Edited displayEdited PICTUREZZ,ZZZ,ZZ9.99-

Test Your Knowledge

1. Best storage for currency?

  • BINARY
  • PACKED-DECIMAL (COMP-3)
  • FLOAT
  • POINTER

2. What does V mean in PICTURE?

  • Visible
  • Implied decimal point
  • Variable
  • Validated

3. How to format output for users?

  • Edited pictures
  • DISPLAY raw
  • BINARY cast
  • MOVE to POINTER

Frequently Asked Questions

Related Pages