How can I right justify an alpha field and make the resulting field numeric?

Document ID : KB000056270
Last Modified Date : 14/02/2018
Show Technical Document Details

The following code provides an example:

     * RIGHT JUSTIFY AN ALPHA FIELD AND MAKE THE RESULTING FIELD NUMERIC
* THERE ARE PROBABLY MORE ELEGANT SOLUTIONS TO RIGHT JUSTIFY AN
      * ALPHA FIELD AND MAKE IT A NUMERIC FIELD BUT THIS ONE WORKS FINE
      * 1. INITIALIZE THE RESULT FIELD TO ZERO
      * 2. TEST EVERY BYTE OF THE FROM FIELD FOR ZERO THRU 9
      * 3. MOVE EVERY NON-BLANK BYTE TO THE RESULT FIELD
      *
      * ALPHA WORKING STORAGE AREA
      * FILE RPT F(81) PRINTER
      DEFINE ALPHA           W 17 A VALUE '123-45-6789'
      DEFINE ONE-BYTE ALPHA     1 A OCCURS 17
      DEFINE ALPHA-SUB       W  2 N 0

      * NEW NUMERIC FIELD WORKING STORAGE AREA
      DEFINE RIGHT-JUSTIFY            W 17 A
      DEFINE RIGHT-NUMBER RIGHT-JUSTIFY 17 N 0
      DEFINE R-ARRAY RIGHT-JUSTIFY   1 A OCCURS 17
      DEFINE RIGHT-SUB       W  2 N 0

      * OTHER WORKING STORAGE
      DEFINE WS-STOPPER     W  2 N
      *
      JOB INPUT NULL
      *
       PERFORM RIGHT-JUSTIFY
       PRINT
       STOP
      *
      RIGHT-JUSTIFY. PROC

       MOVE X'F0' TO RIGHT-JUSTIFY FILL X'F0'
       ALPHA-SUB =  17
       RIGHT-SUB =  17
       WS-STOPPER = 1
      * DO THIS UNTIL YOU REACH THE BEGINNING OF ALPHA FIELD
       DO UNTIL ALPHA-SUB LT WS-STOPPER

      * CHECK FOR BLANK AND MOVE ONE BYTE IF NOT BLANK
      IF ONE-BYTE(ALPHA-SUB) LT '0' OR ONE-BYTE(ALPHA-SUB) GT '9'
             ALPHA-SUB = ALPHA-SUB - 1
          ELSE
             R-ARRAY(RIGHT-SUB) = ONE-BYTE(ALPHA-SUB)
             ALPHA-SUB = ALPHA-SUB - 1
             RIGHT-SUB = RIGHT-SUB - 1
      END-IF

       END-DO

      END-PROC
      *
      *
      REPORT   LINESIZE 80
        TITLE    'ALPHA FIELD \ RIGHT-JUSTIFIED FIELD \ NUMBER FIELD'
        LINE 1   'ALPHA FIELD =============> ' ALPHA
        LINE 2   'RIGHT-JUSTIFIED FIELD ===> ' RIGHT-JUSTIFY
        LINE 3   'JUSTIFIED NUMBER FIELD ==> ' RIGHT-NUMBER

PRODUCES THE FOLLOWING:

ALPHA FIELD =============> 123-45-6789
RIGHT-JUSTIFIED FIELD ===> 00000000123456789
JUSTIFIED NUMBER FIELD ==> 123,456,789