When you need exact fractional values—currency, interest rates, measured quantities with fixed digits—Db2’s DECIMAL type (synonym NUMERIC) is the standard answer. This lesson explains precision and scale, the packed decimal mental model, how DECIMAL differs from binary integers and floating-point, and how COBOL COMP-3 host variables map to these columns.
A decimal number in Db2 is a packed decimal value with an implied decimal point. You declare:
12DECIMAL(precision, scale) NUMERIC(precision, scale) -- synonym
Every value in a DECIMAL column shares the same precision and scale. The range is −n to +n where n is the largest number representable with that p and s (overall max about 10³¹−1 at full precision).
| Declaration | Meaning |
|---|---|
| DECIMAL(5,0) | Five whole digits; no fraction (00000–99999 signed) |
| DECIMAL(9,2) | Nine digits total; two after decimal (e.g. 1234567.89) |
| DECIMAL(31,6) | Maximum precision with six fractional digits |
| NUMERIC(11,2) | Same as DECIMAL(11,2) in Db2 |
For DECIMAL(9,2), think “nine digit slots, two of them after the point.” The integer part can use up to 7 digits. Inserting 12345678.90 fails if it needs 8 integer digits plus 2 scale digits (10 total) against precision 9.
123456789CREATE TABLE ACCOUNT ( ACCT_ID CHAR(10) NOT NULL, BALANCE DECIMAL(11,2) NOT NULL, RATE DECIMAL(5,4), PRIMARY KEY (ACCT_ID) ); INSERT INTO ACCOUNT (ACCT_ID, BALANCE, RATE) VALUES ('0001234567', 1250.50, 0.0325);
Packed decimal stores digits in nibbles (half-bytes). Two decimal digits fit in one byte, with a sign nibble in the final byte. Approximate storage size is INTEGER(p/2)+1 bytes for precision p. You rarely hand-pack these bytes in application code—Db2 and COBOL COMP-3 handle the layout—but the model explains why DECIMAL(5,2) and DECIMAL(15,2) cost different amounts of space.
123456DECIMAL(5,2) value 123.45 (conceptual packing): digits: 1 2 3 4 5 + sign nibble bytes: roughly 3 bytes on disk (INTEGER(5/2)+1) Scale=2 means "decimal point sits before the last two digits."
Because the scale is part of the type, 123.45 and 123.450 are not different “precisions” inside a DECIMAL(5,2) column—the column always carries two fractional digits of scale.
Decimal arithmetic follows Db2 rules for result precision and scale. Results that need more digits than the target column allows cause overflow or require casting. Division and some operations introduce rounding to the result scale. When assigning expression results into DECIMAL columns, confirm the target can hold worst-case magnitudes.
1234567UPDATE ACCOUNT SET BALANCE = BALANCE + DECIMAL('10.00', 11, 2) WHERE ACCT_ID = '0001234567'; -- Explicit cast documents precision/scale for literals and expressions SELECT CAST(BALANCE * RATE AS DECIMAL(13,4)) AS INTEREST_EST FROM ACCOUNT;
COBOL’s packed decimal USAGE COMP-3 is the natural twin of Db2 DECIMAL. Match digit counts carefully:
12345678910* Db2 DECIMAL(11,2) -> 9 integer digits + 2 decimal digits 01 WS-BALANCE PIC S9(9)V99 USAGE COMP-3. 01 WS-RATE PIC S9(1)V9(4) USAGE COMP-3. EXEC SQL SELECT BALANCE, RATE INTO :WS-BALANCE, :WS-RATE FROM ACCOUNT WHERE ACCT_ID = :WS-ACCT-ID END-EXEC.
A classic defect is mapping DECIMAL(11,2) to PIC S9(11)V99 COMP-3 (12 digits before considering how V works) or forgetting the V fractional part. Draw the digit budget on paper until it becomes muscle memory.
DECIMAL is a money jar with labeled slots for coins. If the jar says “two coin slots after the point,” every amount keeps two coin digits—like dollars and cents. Precision is how many digit stickers fit on the jar in total. Packed decimal is a clever way of squeezing two digit stickers into each tiny pocket so the jar does not waste space. INTEGER jars refuse coins; floating jars estimate with fuzzy numbers—DECIMAL keeps exact coin counts.
1. In DECIMAL(p,s), what does s (scale) mean?
2. What is the maximum precision for DECIMAL in Db2 for z/OS?
3. How do DECIMAL and NUMERIC compare in Db2?
4. DECIMAL values are stored as:
5. The usual COBOL mapping for DECIMAL is: