How to make the Compute Result from COBOL cut based on the picture clause that I set

173 Views Asked by At

I'm trying to display a result of computation like add, sub, mul, and div.

My problem is that I set the compute result but it didn't cut. Let's say if I say the number 1 x 1 is 99999x99999, it will produce 10 digits so I must use PIC 9(10) in data division working-storage section. But if 11111x22222 it will produced 9 digits so the result will be 0246908402. There should be a 0 because I set PIC 9(10), but it only produced 9 digits ... if I set PIC 9(6) it will be cut into 908402.

I want to show the result without setting the length of PIC so it will be flexible if it produces 9 digits it will show all 9 digits, if it produces 7 digits it will show all 7 digits without being cut, and without 0 to fill all the spaces. Is it possible? Here's the code

 ******************************************************** 
  DATA DIVISION.                                          
  WORKING-STORAGE SECTION.                                
  01 INBOUND-MESSAGE                  PIC X(14).          
  01 OUTBOUND-MESSAGE                 PIC X(80).          
  01 WS-VARIABLE.                                         
     05 DETAIL1                       PIC X(14)           
        VALUE 'HASIL TAMBAH: '.                           
     05 DETAIL2                       PIC X(14)           
        VALUE 'HASIL KURANG: '.                           
     05 DETAIL3                       PIC X(14)           
        VALUE 'HASIL KALI  : '.                           
     05 DETAIL4                       PIC X(14)           
        VALUE 'HASIL BAGI  : '.                           
     05 WS-NUM1                       PIC 9(5).           
     05 WS-NUM2                       PIC 9(5).           
     05 WS-ADD                        PIC 9(6).           
     05 WS-SUB                        PIC 9(6).           
     05 WS-MUL                        PIC 9(6). -> lets say i set this become 6 so if 11111x22222 it will produced 9  digits and will be cutted into 6 digits          
     05 WS-DIV                        PIC 9(6).           
  01 WS-STRING.                                           
     05 WS-CONCAT                        PIC X(80).       
     05 WS-CONCAT2                       PIC X(80).       
     05 WS-CONCAT3                       PIC X(80).       
     05 WS-CONCAT4                       PIC X(80).       
 ******************************************************** 
  PROCEDURE DIVISION.                                     
       EXEC CICS RECEIVE                              
         INTO (INBOUND-MESSAGE)                     
     END-EXEC.                                      
                                                    
     MOVE INBOUND-MESSAGE(5:5) TO WS-NUM1.          
     MOVE INBOUND-MESSAGE(10:5) TO WS-NUM2.         
     ADD WS-NUM1   TO WS-NUM2 GIVING WS-ADD.        
     SUBTRACT WS-NUM1   FROM WS-NUM2 GIVING WS-SUB. 
     MULTIPLY WS-NUM1 BY WS-NUM2 GIVING WS-MUL.     
     DIVIDE WS-NUM1 BY WS-NUM2 GIVING WS-DIV.       
                                                    
     STRING DETAIL1                                 
            WS-ADD                                  
     DELIMITED BY ':'                               
     INTO WS-CONCAT.                                
     STRING DETAIL2                                 
            WS-SUB                                  
     DELIMITED BY ':'                               
     INTO WS-CONCAT2.                               
     STRING DETAIL3                                 
            WS-MUL                                  
     DELIMITED BY ':'                               
     INTO WS-CONCAT3.                               
     STRING DETAIL4                                 
            WS-DIV                                  
     DELIMITED BY ':'                               
     INTO WS-CONCAT4.                               
     EXEC CICS SEND                                 
         FROM (WS-STRING)                           
         ERASE                                      
         NOHANDLE                                   
     END-EXEC.                                      
       EXEC CICS RETURN  
     END-EXEC.         

any help will be appreciated

2

There are 2 best solutions below

1
On BEST ANSWER

You can use COBOL edited numerics i.e

03 ws-output-number       pic zzzzzzzzz9.

ADD WS-NUM1   TO WS-NUM2 GIVING WS-ADD
move WS-ADD             to ws-output-number

will suppress leading zero's but this would be better.

As Simon pointed out, you can use the FUNCTION TRIM(ws-output-number) to remove leading spaces

03 ws-output-number       pic --,---,---,--9.

as it includes the sign + adds comma's to the output to improve readability

1
On

If I'm understanding your question correctly, you want to suppress the leading zeroes in the result? So given your example, if the maximum possible value of the output is a 10 digit number you would define it as PIC 9(10). If the output of a specific calculation gives the result 0000123456 then you want to display 123456. Ideally, you may want to actually display it as 123,456 or £123,456. This is all achievable using zero suppression and editing techniques which are described in the IBM Enterprise COBOL Reference manual under the section 'zero suppression and replacement editing'. Typically you use a redefine to change the output field to the desired format. So WS-MUL remains PIC 9(10) but is redefined by WS-MUL-X using a PIC Z(9)9 say.