Open In App

Data Type Justification in COBOL

Improve
Improve
Like Article
Like
Save
Share
Report

Data Classification/Types helps the system identify how the programmer wants to use the data in any particular program. In COBOL we have three different Classifications of DATA TYPES, which are Numeric, Alphabetic, and Alpha-Numeric data types.

In COBOL DATA TYPE justification applies to Numeric data type, Alpha-numeric data type, and Alphabetic data type. Among which Numeric Data Type will come under right justified whereas Alphabetic/Alpha-Numeric data type will come under left justified.

Numeric Justification:

If the input data is numbers or digits then it will come under Numeric data types. In COBOL we will use the Numeric data type by using the ‘9(x)’ where x can be any integer, which will represent the size of the data. The maximum value of the numeric data type will be 9(18). The value of numeric justification will get assigned from right to left.  

Alphabetic Justification :

If the input data is alphabetic then it will come under alphabetic data types. In COBOL we will assign the ALPHABETIC DATA TYPE by using ‘A(x)’ where x can be any integer, which will represent the size of the taken data. The maximum size will be A(35535) in COBOL ALPHABETIC DATA TYPE. The value of ALPHABETIC DATA TYPE will get assigned from left to right.

Alpha-Numeric Justification:

If the input data is a mixture of alphabetic and numeric digits then it will come under ALPHA-NUMERIC DATA TYPE. In COBOL we will assign the ALPHA-NUMERIC DATA TYPE by using ‘X(x)’ where x can be any integer, which will represent the size of the taken data. The maximum size will be X(35535) in COBOL ALPHA-NUMERIC DATA TYPE. The value of ALPHA-NUMERIC DATA TYPE will also get assigned from left to right.

Example:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. DATATYPES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VAR011 PIC 9(10) .
01 VAR012 PIC 9(5).
01 VAR021 PIC A(15) .
01 VAR022 PIC A(5).
01 VAR031 PIC X(20) .
01 VAR032 PIC X(13).
PROCEDURE DIVISION.
MAIN-PARA.
    MOVE '123456789' TO VAR011.
    MOVE '123456789' TO VAR012.
    DISPLAY 'NUMERIC DATA JUSTIFICATION'.
    DISPLAY 'VAR011 : ' VAR011.
    DISPLAY 'VAR012 : ' VAR012.
    MOVE 'ABCDEFGHIJKL' TO VAR021.
    MOVE 'ABCDEFGHIJKL' TO VAR022.
    DISPLAY 'ALPHABETIC DATA JUSTIFICATION'.
    DISPLAY 'VAR021 : ' VAR021.
    DISPLAY 'VAR022 : ' VAR022.
    MOVE 'GEEKSFORGEEKS12345' TO VAR031.
    MOVE 'GEEKSFORGEEKS12345' TO VAR032.
    DISPLAY 'ALPHA-NUMERIC DATA JUSTIFICATION'.
    DISPLAY 'VAR031 : ' VAR031.
    DISPLAY 'VAR032 : ' VAR032.
STOP RUN.


Output: 

 

Explanation :  

In the result, we can see two different scenarios for all the data types:

  • For VAR011 we have taken the length of 10, and the input value is ‘123456789’ which is 9 digits, which means less than the length of the variable. As the numeric data type is the right justification, data will assign from right to left so all the data will get inserted and the extra bytes will be covered with leading 0.
  • For VAR012 we have taken the length of 5, which is less than the length of the input variable. As the numeric data type is the right justification, data will assign from right to left so the output will be 56789.
  • For VAR021 we have taken the length of 15, and the input value is ‘ABCDEFGHIJKL’ which is 12 letters and means less than the length of the variable. As the alphabetic data is left justification data will assign from left to right so the output will be ABCDEFGHIJKL and the extra bytes will get ignored.
  • And for VAR022 we have taken the length of 5, which is less than the length of the input variable. As the alphabetic data is left justification data will assign from left to right so the output will be ABCDE.
  •  For VAR031 we have taken the length of 20, and the input value is ‘GEEKSFORGEEKS12345’ which is 18 letters and means less than the length of the variable. As the alphanumeric data is left justification, data will assign from left to right so the output will be GEEKSFORGEEKS12345 and the extra bytes will get ignored.
  • For VAR032 we have taken the length of 13, which is less than the length of the input variable. As in the alphanumeric data is left justification, data will assign from left to right so the output will be GEEKSFORGEEKS .


Last Updated : 18 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads