Packed Decimal Packing Codes (P and Q)

The P and Q packing codes cause decimal values to be stored in so-called packed decimal format. In this format two decimal digits are stored within one 8-bit byte. Each 4-bit nibble contains one binary-coded digit. One nibble is reserved to represent the sign of the number.

For a P packing code, the left-most nibble holds the sign. For a Q packing code, the right-most nibble holds the sign. The sign nibble is 0xc if positive, 0xd if negative. For examples, see the table:

Examples Packed Decimal Format
Packing Code Field Value Hexadecimal Storage
P2 123 c1 23
P3 -1234 d0 12 34
Q3 12 00 01 2c
Q4 -12345 00 12 34 5d

Note:   The Q packing code format is compatible with the COBOL COMP-3 format widely used in legacy data. That is, a COBOL field of PIC S9(n) COMP(3) can be represented by the Uniface packing code Qn.

Range of values

Range of Values for Packing Codes P and Q
Packing codes (no scaling) Range of possible values
P1, Q1 -9 through 9
P2, Q2 -999 through 999
P3, Q3 -99,999 through 99,999
P4, Q4 -9,999,999 through 9,999,999
P5, Q5 -999,999,999 through 999,999,999
P6, Q6 -99,999,999,999 through 99,999,999,999
P7, Q7 -9,999,999,999,999 through 9,999,999,999,999
P8, Q8 -999,999,999,999,999 through 999,999,999,999,999

Scaling

The syntax for scaling with the P and Q packing codes is:

Pm{.n} or Qm{.n}

  • m is the total length of the field to be stored in bytes
  • n is the (optional) number of digits after the decimal. If n is present, a period (.) must separate n from m.
Examples of Scaling With Packing Codes P and Q
Packing codes (with scaling) Range of possible entries
P4.2 -99,999.99 through 99,999.99
Q8.4 -99,999,999,999.9999 through 99,999,999,999.9999
Q7.3 -9,999,999,999.999 through 9,999,999,999.999

Scaling information is not stored with packed data. It is possible, however, to define scaling for a packed field within Uniface. This is available within Uniface only for the following reasons:

  • To assist in the definition of a default display layout.
  • To allow users to take advantage of the packed decimal storage format and still use scaling.

Scaled decimals packed with a P or Q packing code are only stored in this way if the underlying DBMS can handle such a storage format. Other software does not understand scaling with data packed in this way unless it is explicitly programmed to treat the stored number as scaled.

Related Topics