Suppressing leading zeroes

Maxcc0 picture Maxcc0 · Dec 10, 2015 · Viewed 10.2k times · Source

We use a numeric-edited picture clause Z(4).99 to supress leading zeroes. It replaces leading zeros with spaces. My question here is, can we remove those spaces and display only the actual value without leading spaces?

I have ws-amount which is pic 9(09)v99. If I move 100 into it,it will look like 00000010000. But in my report I need it to be displayed like "LOAN AMOUNT $100.00".

Answer

gazzz0x2z picture gazzz0x2z · Dec 11, 2015

Didn't use any functions, as those are site-dependant.

So the result is quite a monster(and I won't spend more time refining it) :

   01  WS.
       05  W-AMOUNT                    PIC 9(09)v99.
       05  W-AMOUNT-ALPHA              REDEFINES W-AMOUNT
                                       PIC X(01) OCCURS 11.
       05  W-DISPLAY                   PIC X(25).
       05  W-DISPLAY-TAB               REDEFINES W-DISPLAY
                                       PIC X(01) OCCURS 25.
       05  W-SENTENCE                  PIC X(12)
                                       VALUE " TO PAY SOON".
       05  W-SENTENCE-TAB              REDEFINES W-SENTENCE
                                       PIC X(01) OCCURS 12.
       05  W-SENTENCE-LENGTH           PIC 9(04) COMP-5.
       05  W-POSITION-AMOUNT           PIC 9(04) COMP-5.
       05  W-POSITION-SENTENCE         PIC 9(04) COMP-5.
       05  W-POSITION-DISPLAY          PIC 9(04) COMP-5.
       05  W-LEADING-ZEROES            PIC X(01).
           88  STILL-LEADING-ZEROES    VALUE "Y".
           88  NO-MORE-LEADING-ZEROES  VALUE "N".

followed by :

   PROCEDURE DIVISION.
  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
   CONTROL-PROCEDURE.
  **
  * The main procedure of the program
  **
       MOVE 15314.16 TO  W-AMOUNT
       PERFORM SUB-PROCEDURE
       DISPLAY  "LOAN AMOUNT $"
       W-DISPLAY
       MOVE 314.16 TO  W-AMOUNT
       PERFORM SUB-PROCEDURE
       DISPLAY  "LOAN AMOUNT $" W-DISPLAY
       GOBACK
       .
   SUB-PROCEDURE.
       MOVE ZERO   TO  W-POSITION-DISPLAY
                       W-POSITION-SENTENCE
       MOVE SPACES TO  W-DISPLAY
       SET STILL-LEADING-ZEROES TO TRUE
       PERFORM VARYING     W-POSITION-AMOUNT
               FROM        1 BY 1
               UNTIL       W-POSITION-AMOUNT > 9
           IF (W-AMOUNT-ALPHA (W-POSITION-AMOUNT)
              NOT EQUAL ZERO) THEN 
              SET NO-MORE-LEADING-ZEROES TO TRUE
           END-IF
           IF NO-MORE-LEADING-ZEROES THEN 
               ADD 1 TO W-POSITION-DISPLAY
               MOVE W-AMOUNT-ALPHA (W-POSITION-AMOUNT)
                 TO W-DISPLAY-TAB  (W-POSITION-DISPLAY)
           END-IF
       END-PERFORM
       ADD 1 TO    W-POSITION-DISPLAY
       MOVE "." TO W-DISPLAY-TAB (W-POSITION-DISPLAY)
       ADD 1 TO    W-POSITION-DISPLAY
       MOVE        W-AMOUNT-ALPHA (W-POSITION-AMOUNT)
         TO        W-DISPLAY-TAB  (W-POSITION-DISPLAY)
       ADD 1 TO    W-POSITION-DISPLAY
                   W-POSITION-AMOUNT
       MOVE        W-AMOUNT-ALPHA (W-POSITION-AMOUNT)
         TO        W-DISPLAY-TAB  (W-POSITION-DISPLAY)
       MOVE LENGTH OF W-SENTENCE TO W-SENTENCE-LENGTH
       PERFORM W-SENTENCE-LENGTH TIMES
           ADD  1  TO  W-POSITION-DISPLAY
                       W-POSITION-SENTENCE
           MOVE W-SENTENCE-TAB     (W-POSITION-SENTENCE)
             TO W-DISPLAY-TAB      (W-POSITION-DISPLAY)
       END-PERFORM
       .

final result :

LOAN AMOUNT $15314.16 TO PAY SOON     
LOAN AMOUNT $314.16 TO PAY SOON  

There are plenty of ways optimizing it(especially the way I'm handling digits behind the dot, it's absolutely awful, but I'm doing it while watching other things, so I went to simpler things). I would not dare submitting that to code review. But it works.