Data Type Justifications


Data justification refers to the alignment of data within a variable. By default, numeric data is right-justified, while alphabetic and alphanumeric data is left-justified.

However, COBOL provides additional features to adjust this alignment for alphabetic and alphanumeric data types. The JUSTIFIED clause overrides the default alignment for these data types.

Numeric Data Type -


Numeric data type is right justified by default, and no override alignment applies to it. However, below are the possible scenarios while moving the data from one to another variable -

Case-1: - Input data is larger than the declared variable size. (Overflow)

If the input data exceeds the declared variable size, the digits move from right to left, and the extra digits are ignored. The variable can only have digits that are equal to its size.

Case-2: - Input data is smaller than the declared variable size.

If the input data is smaller than the declared variable size, the digits are assigned to the variable from right to left. The remaining digits on the left side of the variable are filled with ZEROES.

Example -

Scenario - Describes how the numeric data aligns in COBOL variables.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       ...
       WORKING-STORAGE SECTION. 
       01  WS-VAR.
           05 NUMERIC-J1        PIC 9(03).
           05 NUMERIC-J2        PIC 9(09).
       ...
       PROCEDURE DIVISION.
           MOVE 256128          TO  NUMERIC-J1
                                    NUMERIC-J2.
           DISPLAY 'NUMERIC-J1:  ' NUMERIC-J1.
           DISPLAY 'NUMERIC-J2:  ' NUMERIC-J2.
		   ...

Output -

NUMERIC-J1:  128         
NUMERIC-J2:  000256128   

Alphabetic | Alphanumeric Data Type -


Alphabetic or Alphanumeric data type is left justified by default, and the JUSTIFIED clause overrides the default alignment. However, below are the possible scenarios while moving the data from one to another variable -

Case-1: - Input data is larger than the declared variable size. (Overflow)

If the input data exceeds the declared variable size, the characters move from left to right, and the extra characters are ignored. The variable can only have characters that are equal to its size.

Case-2: - Input data is smaller than the declared variable size.

If the input data is smaller than the declared variable size, the characters are assigned to the variable from left to right. The remaining characters on the right side of the variable are filled with LOW-VALUES or HIGH-VALUES.

Case-3: - Overriding JUSTIFIED clause.

JUSTIFIED clause overrides the default left alignment and applies right alignment to it.

Syntax -

 level-number ws-variable   
         PIC data-type(variable-length) JUSTIFIED RIGHT.

Example -

Scenario - Describes how the non-numeric data aligns in COBOL variables.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       ... 
       WORKING-STORAGE SECTION.
       01  WS-VAR.
           05 ALPHA-J1          PIC A(10).
           05 ALPHA-J2          PIC A(50).
		   05 ALPHA-JR          PIC A(50) JUSTIFIED RIGHT.
       ... 
       PROCEDURE DIVISION.
           MOVE "MAINFRAMES IS LEGACY SYSTEM"   TO ALPHA-J1 
                                                   ALPHA-J2
												   ALPHA-JR.
           DISPLAY 'ALPHA-J1:  ' ALPHA-J1.
           DISPLAY 'ALPHA-J2:  ' ALPHA-J2.
		   DISPLAY 'ALPHA-JR:  ' ALPHA-JR.
		   ...

Output -

ALPHA-J1:  MAINFRAMES                                        
ALPHA-J2:  MAINFRAMES IS LEGACY SYSTEM                       
ALPHA-JR:                         MAINFRAMES IS LEGACY SYSTEM

Explaining Example -

ALPHA-J1 is declared with a length of ten, and ALPHA-J2 is declared with a length of 20. "MAINFRAMES IS LEGACY SYSTEM" value moved to both fields.

After moving, ALPHA-J1 is smaller than the value passed. Because of left justification, the value should move from left to right up to the length declared. So ALPHA-J1 had the value MAINFRAMES.

After moving, ALPHA-J2 is larger than the value passed. Because of left justification, the value should move from left to right up to the length declared. So ALPHA-J2 had the value "MAINFRAMES IS LEGACY SYSTEM".

After moving, ALPHA-JR is larger than the value passed. Because of right justification (JUSTIFIED clause), the value should move from right to left up to the length declared. So ALPHA-JR had the value "MAINFRAMES IS LEGACY SYSTEM".