filecobolcomp-3

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


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


Solution

  • 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$