Skip to content
systemcoding.dev
Mainframe

Declare a signed packed decimal with PIC S9(7)V99 COMP-3

Declare a signed packed decimal with PIC S9(7)V99 COMP-3, which takes five bytes, and see how a value is stored.

1 min read
Summarize in AI

COMP-3 stores two digits per byte and puts the sign in the last half-byte. In Enterprise COBOL for z/OS 6.4, PIC S9(7)V99 COMP-3 has nine digits, which need five bytes: nine digit nibbles and the sign nibble.

cobol
       WORKING-STORAGE SECTION.
       01  WS-AMOUNT  PIC S9(7)V99 COMP-3 VALUE -1234.56.
       PROCEDURE DIVISION.
           DISPLAY 'LENGTH ' LENGTH OF WS-AMOUNT
           DISPLAY 'VALUE  ' WS-AMOUNT
           STOP RUN.
Text
LENGTH 5
VALUE  -000001234.56

The stored bytes are X'000123456D': digits 000123456 and the sign nibble D for negative. The V marks an implied decimal point that takes no space, so the field holds the value as the integer 123456 and the compiler remembers the scale.

The rule for the byte count is digits plus one, divided by two and rounded up. A PIC with an even number of digits gets a wasted leading half-byte, so an odd count wastes nothing. COMP-3 is the default choice for money on z/OS, while on a PC the exact decimal behaviour would have to come from a library.

Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.