COBOL Tutorial

Progress0 of 0 lessons

COBOL Numeric Data Types

In COBOL, numeric data is defined with the PICTURE clause using the symbols 9 (digit) and S (sign), and optionally V (implied decimal point). The way the value is stored in memory is determined by USAGE (DISPLAY, COMP, COMP-3, etc.). This page explains numeric data types: integer vs decimal, signed vs unsigned, and how to choose the right PICTURE and USAGE for numbers.

Explain Like I'm Five: Numeric Data Types

Numbers in a program can be whole (like 1, 2, 100) or have a decimal part (like 3.14 or 99.99). COBOL lets you say "this is a whole number" (no decimal) or "this number has two digits after the decimal" (e.g. dollars and cents). You also say whether the number can be negative (like a balance that can go below zero). The type you choose affects how much space it uses and how the computer does math with it.

PICTURE Symbols for Numeric Data

The PICTURE (or PIC) clause defines the shape and meaning of the data. For numeric fields you use 9 for digits and optionally S for sign and V for decimal position.

Numeric PICTURE symbols
SymbolMeaningExample
9One numeric digit (0–9)PIC 9(4) = four digits
S9Signed digit; field can be negativePIC S9(5) = signed 5-digit integer
VImplied decimal point (no storage)PIC 9(3)V99 = 3 integer, 2 decimal places
PScaling (decimal positions at end)PIC P(3) = scale factor (less common)

The number in parentheses (e.g. 9(5)) means that many occurrences. So 9(5) is five digits; S9(7)V99 is seven digits before the decimal and two after, with sign.

cobol
1
2
3
4
01 WS-COUNT PIC 9(5). *> Unsigned, 5 digits (e.g. 0 to 99999) 01 WS-AMOUNT PIC S9(7)V99. *> Signed, 7 digits + 2 decimals (e.g. -9999999.99 to 9999999.99) 01 WS-QUANTITY PIC S9(4). *> Signed integer, 4 digits 01 WS-RATE PIC 9(2)V99. *> Unsigned, 2 integer + 2 decimal (e.g. 0.00 to 99.99)

The Implied Decimal Point (V)

V stands for the implied (assumed) decimal point. It does not take any storage; it only tells the compiler and runtime where the decimal point is when interpreting the value. For example, PIC 9(3)V99 means three digits to the left of the decimal and two to the right. The value 12345 in that field is interpreted as 123.45. When you do arithmetic, the decimal alignment is handled according to the V position.

So 9(5)V2 and 9(7)V2 both have two decimal places; the first has five total digit positions (three integer, two decimal) and the second has seven (five integer, two decimal). The number of 9s (and S if present) determines the total digit capacity; V only fixes where the decimal point is.

Signed (S9) vs Unsigned (9)

PIC 9 means the field holds only non-negative values (zero and positive). PIC S9 means the field can hold negative values as well. The S is stored in a sign position: in DISPLAY it is combined with the rightmost digit (overpunch); in COMP it is the high-order bit; in COMP-3 it is the last half-byte (nibble). Use S9 for balances, adjustments, differences, or any value that can be negative.

USAGE: How the Number Is Stored

The same PICTURE can be stored in different ways. USAGE (or the shorthand in the PICTURE) controls the internal format and size.

USAGE and storage for numeric data
USAGEStorageTypical use
DISPLAY1 byte per digitI/O, screens, reports; human-readable
COMP / COMP-42, 4, or 8 bytes (binary)Integer arithmetic, counters, IDs
COMP-3(n+1)/2 bytes (packed)Money, quantities, exact decimal
COMP-1 / COMP-24 or 8 bytes (float)Scientific; approximate; avoid for money

DISPLAY (default)

Each digit is stored as a character (one byte per digit). The value is human-readable in memory. Good for data that comes from or goes to files or screens in character form. Arithmetic is possible but slower than COMP or COMP-3 because each digit is handled separately.

COMP / COMP-4 (binary)

The value is stored in binary. Size depends on the number of digits: 1–4 digits use 2 bytes, 5–9 use 4 bytes, 10–18 use 8 bytes. Range for 2 bytes is -32,768 to 32,767; for 4 bytes roughly ±2 billion. No decimal places in storage; use COMP for integers (counters, IDs, subscripts). Fast for integer arithmetic.

COMP-3 (packed decimal)

Two decimal digits per byte (each half-byte holds one digit), with the sign in the last nibble (C for positive, D for negative). Exact decimal arithmetic; no rounding from binary. Standard for money and quantities. Size is (number of digits + 1) / 2 bytes (the +1 is for the sign). Example: S9(7)V99 has 9 digit positions, so (9+1)/2 = 5 bytes.

Integer vs Decimal Numeric Fields

For whole numbers (no decimal part), use a PICTURE without V: e.g. 9(5) or S9(4). For values with decimal places (money, rates), use V: e.g. S9(7)V99. The number of 9s before V is the integer part; the number of 9s after V is the decimal part. Choose the sizes so the largest (and smallest, if signed) value you need fits.

cobol
1
2
3
4
5
6
7
*> Integer: no V 01 WS-REC-COUNT PIC 9(6). *> 0 to 999999 01 WS-OFFSET PIC S9(4) COMP. *> -32768 to 32767 (2-byte binary) *> Decimal: with V 01 WS-BALANCE PIC S9(9)V99 COMP-3. *> Up to 9 digits + 2 decimals 01 WS-PERCENT PIC 9(2)V99. *> 0.00 to 99.99 (e.g. percentage)

Ranges and Sizes

For DISPLAY and COMP-3, the range is determined by the number of digits: n digits give 0 to 10^n - 1 (unsigned) or -(10^n - 1) to 10^n - 1 (signed). For COMP, the range is that of the binary size: 2-byte signed is -32,768 to 32,767; 4-byte is about ±2.1 billion. Be sure the values you store fit in the chosen PICTURE and USAGE.

Step-by-Step: Defining a Numeric Field

  • Decide if the value can be negative; if yes, use S9, otherwise 9.
  • Decide how many digits you need before and after the decimal. If no decimal part, use only 9(n) or S9(n). If there is a decimal part, use V: e.g. S9(7)V99 for 7 integer digits and 2 decimal places.
  • Choose USAGE: DISPLAY for character I/O, COMP for integers and fast math, COMP-3 for money or exact decimal.
  • Define the field in WORKING-STORAGE or the appropriate section (e.g. FILE SECTION for record layout).

Best Practices

  • Use COMP-3 for money and any value that must have exact decimal behavior.
  • Use COMP for integer-only data (counts, IDs) when you want fast arithmetic.
  • Use DISPLAY for fields that are read from or written to external sources in character form.
  • Use S9 when the value can be negative; use 9 when it is always non-negative.
  • Size the PICTURE to the maximum number of digits (and decimals) you need.

Practice Exercises

Exercise 1

Define a working-storage field for a customer balance that can be negative, up to 999,999.99. Use COMP-3.

Exercise 2

Define a counter that will go from 0 to 99,999. Use COMP. How many bytes does it use?

Exercise 3

Write a PICTURE for a signed quantity with 5 integer digits and 3 decimal places. Which USAGE would you use for a stock quantity?

Test Your Knowledge

1. What does the S in PIC S9(5) mean?

  • String type
  • Signed – the field can hold negative values
  • Space character
  • Size in bytes

2. How is the decimal position defined for a numeric field?

  • With a period in the PICTURE
  • With V (implied decimal point) – no storage, only position
  • With a separate DECIMAL clause
  • With COMP-3 only

3. PIC S9(5)V99 COMP-3 uses how many bytes?

  • 5
  • 4
  • 6
  • 7

4. Which USAGE is typically used for money amounts?

  • COMP (binary)
  • COMP-3 (packed decimal)
  • COMP-1 (float)
  • DISPLAY only

Related Concepts

Related Pages