In COBOL, how to convert sign comp-3 value to a readable format along with sign

20.9k views Asked by At

I have come across a situation where the value coming in from record is in below format 01 WS-PREMIUM PIC S9(05)V9(02) comp-3.

As we know in comp-3, sign is stored in last nibble. e.g. WS-PREMIUM having value +1234.10 will be stored in as x'0123410C' or WS-PREMIUM having value -1234.10 will be stored in as x'0123410D'.

Now i want to write this to a Report file((Lets say Daily Premium file). This value should be written to file with its sign as +1234.10$ or -1234.10$. I was searching this forum for the answer but what could i found is the pre-defined sign variable in the report section whereas what i am looking for is run time identification of sign by looking at last nibble and write to a file accordingly. I also heard there is a way they do this in java but not sure how.

I know, when we use DISPLAY it shows you unpacked decimal with its sign byte.

can someone please help me with this. Thanks in advance.

Here is the sample code i used -

   IDENTIFICATION DIVISION.
    PROGRAM-ID. V1329006.

   ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
        SELECT OPDATA ASSIGN TO "OPDATA.DAT"
         ORGANIZATION IS LINE SEQUENTIAL. 
   DATA DIVISION. 
    FILE SECTION.
    FD OPDATA.
    01 WS-OP-RECORD PIC X(20).

    WORKING-STORAGE SECTION.

    01 HEADER-LINE.
        05 FILLER     PIC X(08) VALUE 'DATE'.
        05 FILLER     PIC X(01).
        05 FILLER     PIC X(08) VALUE 'PREMIUM'.

    01 DETAIL-LINE.
        05 WS-DATE    PIC 9(08) VALUE '20181119'.
        05 FILLER     PIC X(01).
        05 WS-PREMIUM PIC S9(05)V9(02) comp-3.
    01 WS-INPUTS.

        05 WS-EARNED-PREMIUM    PIC S9(05)V9(02).

        05 WS-RETURN-PREMIUM    PIC S9(05)V9(02).

   PROCEDURE DIVISION.
       OPEN OUTPUT OPDATA. 
       MOVE '+1234.10' TO WS-EARNED-PREMIUM
       MOVE '-10.05' TO WS-RETURN-PREMIUM
       COMPUTE WS-PREMIUM = 
         WS-EARNED-PREMIUM + WS-RETURN-PREMIUM
        DISPLAY 'WS-PREMIUM='WS-PREMIUM 
        WRITE WS-OP-RECORD FROM HEADER-LINE
        WRITE WS-OP-RECORD FROM DETAIL-LINE
       CLOSE OPDATA.
    GOBACK.
    END PROGRAM V1329006.

I am expecting out put to be shown as

DATE PREMIUM20181119 +1224.05

2

There are 2 answers

5
Srinivasan JV On BEST ANSWER

You might have to check PICTURE clause editing in COBOL.

Some useful links: PICTURE Clause Editing and Edited Pictures.

A sample code snippet is shown below.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION. 
WORKING-STORAGE SECTION. 
01 WS-A PIC S9(05)V9(02) comp-3 VALUE -1234.10.
01 WS-GROUP.
   05 WS-B PIC +99999.99.
   05 WS-C PIC X VALUE '$'.
PROCEDURE DIVISION.
MOVE WS-A TO WS-B.
DISPLAY WS-GROUP. 
STOP RUN.

Output:

-01234.10$
1
James On

If you want a packed-decimal number to be human-readable with a sign then you will need to convert it into another format, a numeric-edited format.

I have tried the following on IBM mainframe COBOL, and got these results.

01 DETAIL-LINE.                            
    05 WS-DATE    PIC 9(08) VALUE 20181119.
    05 FILLER     PIC X(01).               
    05 WS-PREMIUM PIC S9(05)V9(02) comp-3. 
    05 filler pic x value '$'.             

    DISPLAY DETAIL-LINE.

DATE     PREMIUM   
20181119    *$     

That's packed-decimal with an implicit decimal point and sign in the last half byte.

01 DETAIL-LINE2.                                          
    05 WS-DATE2   PIC 9(08) VALUE 20181119.               
    05 FILLER     PIC X(01).                              
    05 WS-PREMIUM2 PIC S9(05)V9(02) SIGN LEADING SEPARATE.
    05 filler pic x value '$'.                            

    DISPLAY DETAIL-LINE2.
DATE     PREMIUM
20181119 +0122405$

So here we get the sign, and it is readable, but we don't get a decimal point and we have a leading zero.

01 DETAIL-LINE3.                           
    05 WS-DATE3   PIC 9(08) VALUE 20181119.
    05 FILLER     PIC X(01).               
    05 WS-PREMIUM3 PIC +9(05).9(02).       
    05 filler pic x value '$'.             

    DISPLAY DETAIL-LINE4.
DATE     PREMIUM
20181119 +01224.05$

So now we get the decimal point, but still have a leading zero.

01 DETAIL-LINE4.                            
    05 WS-DATE4   PIC 9(08) VALUE 20181119.
    05 FILLER     PIC X(01).               
    05 WS-PREMIUM4 PIC +Z(05).9(02).       
    05 filler pic x value '$'.             

    DISPLAY DETAIL-LINE4.

DATE     PREMIUM
20181119 + 1224.05$

So now we don't have a leading zero (but do have a leading space).

Thanks to @NicC for the next part.

01 DETAIL-LINE5.                           
    05 WS-DATE5   PIC 9(08) VALUE 20181119.
    05 FILLER     PIC X(01).               
    05 WS-PREMIUM5 PIC +++++9.9(02).       
    05 filler pic x value '$'.             

    DISPLAY DETAIL-LINE5.

DATE     PREMIUM
20181119  +1224.05$

So with the extra signs, the sign becomes floating so you don't get spaces any more (or leading zeroes).