Search code examples
cobolcics

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


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


Solution

  • 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