Menu
CoddyTech

Padding

Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 8 of 23.

When a value is shorter than its field, MOVE fills the rest. What it fills with depends on the receiving field.

A numeric field is padded with zeros on both sides of the point: 7.5 moved into PIC 9(3)V99 is 00750.

A PIC X field gets the value from the left and spaces on the right. A number moved into one keeps all its digits, leading zeros included:

01 QTY PIC 9(3) VALUE 42.
01 QTY-TEXT PIC X(5).
    MOVE QTY TO QTY-TEXT. *> "042  "

Padding keeps every field at its full width, which is what lines up the columns of a report. A field never grows or shrinks.

challenge icon

Challenge

Easy

CUST-NO is PIC X(6), so customer 42 prints as Customer 42. Change its picture so numbers are padded with zeros to 6 digits: Customer 000042.

Example input, one line per ACCEPT: 42.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CUSTNO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 CUST-NO PIC X(6).
       PROCEDURE DIVISION.
           ACCEPT CUST-NO.
           DISPLAY "Customer " CUST-NO.
           STOP RUN.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Numbers