Packed decimal
A compact way to store a number: two digits in each byte. Banks use it for money.
Also written COMP-3, comp-3, decimal
Why it matters. Amounts in bank files are packed to save space. In COBOL you ask for it with COMP-3.
See it
Seven digits and a sign. The decimal point is not stored.
Syntax
field-name PIC S9(n)V9(m) COMP-3. field-name PIC S9(n)V9(m) PACKED-DECIMAL.
- COMP-3
- Store as packed decimal.
- PACKED-DECIMAL
- The same thing, written in full.
- S
- Include a sign. Without it the sign nibble is F, meaning unsigned.
- bytes
- (digits + 1) divided by 2, rounded up.
Try it COMP-3
- 1
In the program PACK, declare WS-AMT as a signed amount: 5 digits, then 2 decimals, stored packed, starting at 1200.50.
COBOL 01 WS-AMT PIC S9(5)V99 COMP-3 VALUE 1200.50. - 2
Run PACK. It shows how many bytes WS-AMT takes, and how many a normal field takes.
RUNCOBOL 'ZB0001.DEV.COBOL(PACK)'
Remember
- Packed stores two digits in each byte, with the sign in the last half-byte.
- PIC S9(5)V99 COMP-3 takes 4 bytes: add 1 to the digits and halve, rounding up. Without COMP-3 it takes 7.
- Banks use it for money: it saves space and the arithmetic is exact.
The usual mistake
PIC S9(5)V99 for a field that holds packed data in the file
The program reads each byte as one digit. The numbers come out wrong, or the program ends with S0C7.
PIC S9(5)V99 COMP-3, so the program knows the data is packed
On real z/OS
MF/OS reports the packed size (4 bytes for S9(5)V99) but holds the value as decimal text, so you can't look at the packed bytes or the C, D and F sign nibble.
Where can I practise Packed decimal?
Right here, at ZedBank. You take a junior job at a simulated bank and use Packed decimal in real work, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- PICTURE clauseThe COBOL declaration that says what kind of data a field holds and how long it is, written as PIC.
- DATA DIVISIONThe part of a COBOL program that declares every record layout and variable it will use.
- COBOLThe programming language most business logic on the mainframe is written in. Verbose by design, and still running the world's payments.
- EBCDICThe character encoding mainframes use, where letters and numbers are stored as different byte values than on a laptop.
- CopybookA shared block of COBOL declarations kept in a library and included into programs, so a record layout is defined once.