Packed decimal—type P in Easytrieve, COMP-3 in COBOL—is how mainframe financial systems store money efficiently. Two digits squeeze into each byte except the last nybble, which carries the sign. A five-byte packed field can hold up to nine digits plus sign, far denser than one digit per byte zoned format. Payroll files, general ledger extracts, and invoice amounts overwhelmingly use P. Declare wrong length or decimal count and every field after the mistake reads misaligned bytes. This page teaches packed nybble layout, sign codes, EVEN option, decimal positions, and debugging with hex display.
Each byte holds two decimal digits in nybbles (four bits each). The rightmost nybble of the last byte is the sign: F or C often positive, D negative in IBM conventions. Example: +123 in two bytes encodes as X'123F'. -123 encodes as X'123D'. The implied decimal point is not stored—it comes from decimal positions on DEFINE. P 3 2 might store 12345 representing 123.45.
123DEFINE AMOUNT W 5 P 2 VALUE 1234.56 * Five bytes packed, two decimal places DISPLAY HEX AMOUNT
Syntax: field-name, location, length, P, decimal-positions. SALARY 40 5 P 2 at byte forty, five bytes, two decimals. Working storage: WS-TOTAL W 7 P 2. Decimal positions 0 through eighteen. Specifying decimals makes field quantitative for automatic SUM on reports.
| Value | 2-byte packed hex | Note |
|---|---|---|
| +123 | X'123F' | Positive sign F |
| -123 | X'123D' | Negative sign D |
| +0 | X'000C' or similar | Zero patterns vary by context |
| Digits only | Pairs per byte | Last nybble is sign |
Maximum P length ten bytes supports up to eighteen decimal digits with sign in last nybble— reference tables tie length to digit capacity and max decimal positions. A three-byte packed field holds fewer digits than seven-byte. Planning salary fields: estimate maximum amount with decimals, choose length that fits, verify against copybook COMP-3 length formula (digits + 1) / 2 rounded up for byte count.
EVEN on DEFINE for P fields forces even digit count with high-order zero digit. Two-byte even packed holds exactly two digits such as X'012F' for 12. Use when file layout or external system requires even digit packed fields—uncommon but documented. EVEN valid only on P—not on N or A.
P when file is COMP-3 from COBOL or DB2 decimal export to packed. N when file is character numeric PIC 9 or zoned USAGE DISPLAY. P saves space and matches financial batch pipelines. N easier for human-readable DISPLAY without hex. Converting between them in assignment works but costs CPU in tight loops—match source format first.
ADD, subtract, multiply, divide in expressions use packed internal math when operands are P. WS-TAX = GROSS * 0.15 ROUNDED respects decimal positions on receive field. SUM GROSS on REPORT accumulates quantitative P fields. Watch overflow when result exceeds field capacity— test maximum wage amounts and bonus caps.
Default masks exist by P length and decimals. Custom MASK '$$$,$$9.99-' on DEFINE formats report columns. BWZ suppresses line when amount zero—common on detail lines, not always on totals. MASK does not alter packed bytes in storage.
COBOL PIC S9(7)V99 COMP-3 typically maps to P length 5 with 2 decimals—seven digits plus two decimal digits fit five packed bytes. Always verify with copybook byte offset. SYNC and REDEFINES in COBOL may alter alignment—Easytrieve FILE must match physical record not just logical group picture.
Packed decimal is putting two digit stickers in each box slot except the last sticker spot which tells if the number is plus or minus. You save box space compared to one digit per box. The decimal point is agreed in the rule book (decimal positions) not drawn on the stickers. If you use the wrong size box, every sticker after that sits in the wrong slot.
1. Packed decimal in Easytrieve uses type letter:
2. Value 123 in two-byte packed often appears as hex:
3. EVEN on DEFINE applies to:
4. Maximum packed field length documented is:
5. Negative 123 packed sign nybble is often: