COBOL GnuCOBOL 3.2 Β·
β verified by execution on 2026-08-16
Data conversion in COBOL happens almost entirely behind the scenes, driven by the MOVE statement and the PICTURE clauses of the fields involved. Unlike languages where you must explicitly call functions like parseInt() or str(), COBOL automatically converts, pads, and truncates data based on the definition of the receiving item. This implicit conversion is powerful but can lead to subtle bugs if you donβt understand the rules.
In this lesson, we will explore exactly what happens when data is moved between different representations, how numeric data aligns, how alphanumeric data pads or truncates, and the special rules that govern group moves.
The Alphanumeric Padding Rule
When moving a shorter string to a longer alphanumeric field, COBOL pads the remainder with spaces on the right side. This ensures that the entire receiving field is initialized and no residual βgarbageβ data is left behind.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. ExAlphaPad. DATA DIVISION. WORKING-STORAGE SECTION. 01 SHORT-VAL PIC X(2) VALUE "AB". 01 LONG-VAL PIC X(5). PROCEDURE DIVISION. MOVE SHORT-VAL TO LONG-VAL. DISPLAY "*" LONG-VAL "*". STOP RUN.
Output
*AB *
Your output
The Alphanumeric Truncation Rule
Conversely, when moving a larger string into a smaller alphanumeric field, COBOL simply truncates the excess characters on the right. The compiler will often warn about possible field truncation if it detects this at compile time.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. ExAlphaTrunc. DATA DIVISION. WORKING-STORAGE SECTION. 01 LONG-VAL PIC X(5) VALUE "ABCDE". 01 SHORT-VAL PIC X(2). PROCEDURE DIVISION. MOVE LONG-VAL TO SHORT-VAL. DISPLAY SHORT-VAL. STOP RUN.
Output
AB
Your output
Numeric Alignment and Truncation
Numeric moves behave differently. Data is moved from right to left, typically aligning by the decimal point. If the receiving field is smaller, high-order digits may be truncated, which can silently alter the magnitude of the number. If the field has more integer positions, it pads with zeros on the left.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. ExNumTrunc. DATA DIVISION. WORKING-STORAGE SECTION. 01 BIG-NUM PIC 9(4) VALUE 1234. 01 SMALL-NUM PIC 9(2). PROCEDURE DIVISION. MOVE BIG-NUM TO SMALL-NUM. DISPLAY SMALL-NUM. STOP RUN.
Output
34
Your output
Group Moves
One of the most frequent sources of bugs in COBOL is the group move. A group move operates without data conversion, moving left to right byte by byte, ignoring the internal definitions of the elementary items. It is treated exactly like an alphanumeric to alphanumeric move.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. ExGrpMove. DATA DIVISION. WORKING-STORAGE SECTION. 01 GRP-1. 05 NUM-1 PIC 9(2) VALUE 12. 05 ALPHA-1 PIC X(2) VALUE "AB". 01 GRP-2 PIC X(4). PROCEDURE DIVISION. MOVE GRP-1 TO GRP-2. DISPLAY GRP-2. STOP RUN.
Output
12AB
Your output
Common Misconceptions
A very common mistake is assuming that moving a numeric value to an alphanumeric field automatically formats it nicely. The MOVE statement operates left to right, byte by byte in this context, similar to IBM COBOL. If you move a plain numeric item to an alphanumeric item, it just drops the bytes in.
To properly format numbers for display, you must use numeric-edited fields. Letβs see what happens if you try to move a raw numeric field directly. What do you think the output will be?
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
IDENTIFICATION DIVISION. PROGRAM-ID. ExNumToAlpha. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM-VAL PIC 9(4) VALUE 1234. 01 ALPHA-VAL PIC X(4). PROCEDURE DIVISION. MOVE NUM-VAL TO ALPHA-VAL. DISPLAY ALPHA-VAL. STOP RUN.
Output
1234
You predicted
Using a numeric-edited picture clause is the correct way to convert a numeric value into a readable string:
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. ExNumEdit. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM-VAL PIC 9(4)V99 VALUE 1234.56. 01 EDITED-VAL PIC $$,$$9.99. PROCEDURE DIVISION. MOVE NUM-VAL TO EDITED-VAL. DISPLAY EDITED-VAL. STOP RUN.
Output
$1,234.56
Your output
Memory Layout and Truncation
To visualize how alphanumeric truncation works in memory, consider this diagram. It shows a 6-byte source field being moved into a 4-byte receiving field. The first 4 bytes are transferred, and the remaining 2 bytes are discarded.
Alphanumeric truncation drops bytes from the right side of the field.
By understanding these conversion rules, you can avoid common pitfalls such as high-order digit truncation or incorrect group move behavior, resulting in more robust and predictable COBOL programs.
Check yourself
What happens when you move a 3-character string into a 5-character alphanumeric PIC X(5) field?
Reveal answer
The string is padded with spaces on the right. β COBOL pads alphanumeric receiving fields with spaces on the right to fill the remaining length.
Which statement about group moves is true?
Reveal answer
They behave like an alphanumeric to alphanumeric move, byte by byte. β Group moves ignore the internal data definitions and move data straight across, byte by byte, left to right.
If you move 1234 into a PIC 9(3) field, what happens?
Reveal answer
The value becomes 234. β Numeric moves align at the decimal. Excess high-order digits on the left are truncated, leaving 234.
Challenges
π Bug Hunt+15 XP
A developer wants to format the employee ID. But when printed, the ID is missing its first two digits! Fix the `EMPLOYEE-PRINT` definition so it doesn't truncate the 6-digit ID.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it β the tests below decide when you are right.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "123456\n"
Show solution (0 XP)
IDENTIFICATION DIVISION. PROGRAM-ID. BugHuntTrunc. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-ID PIC 9(6) VALUE 123456. 01 EMPLOYEE-PRINT PIC 9(6). PROCEDURE DIVISION. MOVE EMPLOYEE-ID TO EMPLOYEE-PRINT. DISPLAY EMPLOYEE-PRINT. STOP RUN.
Challenge 2 +15 XP
The raw sales amount is `004567`. We want to print it as a dollar amount with a decimal point and leading zero suppression. Use `MOVE` to format `RAW-SALES` into `FORMATTED-SALES` and print it.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects " $45.67\n"
Show solution (0 XP)
IDENTIFICATION DIVISION. PROGRAM-ID. CConvertEdit. DATA DIVISION. WORKING-STORAGE SECTION. 01 RAW-SALES PIC 9(4)V99 VALUE 0045.67. 01 FORMATTED-SALES PIC $$$9.99. PROCEDURE DIVISION. MOVE RAW-SALES TO FORMATTED-SALES. DISPLAY FORMATTED-SALES. STOP RUN.