0

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

VinDesai
  • 13
  • 1
  • 1
  • 4
  • 1
    Please include the source code that you have tried, what results that gives, and what your expected result should look like. Also include sufficient data values. From the above I cannot tell what is working and what is not. – donPablo Nov 19 '18 at 05:50
  • I have added sample code which i used and the result should look like - DATE PREMIUM 20181119 +1224.05 – VinDesai Nov 19 '18 at 06:31
  • FYI, I've just fixed the link for sample code snippet. – Srinivasan JV Nov 19 '18 at 06:37
  • 1
    Hi @VinDesai, in the code snippet which you've shown above, it looks like `01 DETAIL-LINE` group item is being used to `WRITE` to the output file. Why don't you simply remove `COMP-3` and replace it with a numeric edited PICTURE clause like `+99999.99`? – Srinivasan JV Nov 19 '18 at 06:55

2 Answers2

4

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$
Srinivasan JV
  • 705
  • 5
  • 12
  • Thank you for your help. What i am looking is to write comp-3 in file. DISPLAY clause will anyway unpack and shows it. I am more concern about writing it to file. Also i cannot define sign + or - in program since it will be depending on the computational values. We may end up getting negative results which should be printed in file. – VinDesai Nov 19 '18 at 06:39
  • 1
    Hi @VinDesai I believe you can just write the PICTURE clause edited field to the output file. For sign, you may prefer using + symbol in the PIC clause of the edited field. + is printed if the value is positive and - is printed if the value is negative. In the example that I've shown, the PIC clause of numeric edited field (WS-B) has got +ve sign, whereas the input value is -ve. The output shows -ve. Please let me know if you need more details. – Srinivasan JV Nov 19 '18 at 06:52
  • @piet.t As per your suggestion, I've added the example code in the answer rather than links. Thanks! – Srinivasan JV Nov 19 '18 at 07:30
  • 1
    Thank you @SrinivasanJv . I used numeric-edited with chain of signs that gave me desired results. Thank you so much for your help. – VinDesai Nov 19 '18 at 11:09
  • I'm so glad that it helped. Thanks! – Srinivasan JV Nov 19 '18 at 11:41
1

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).

James
  • 331
  • 2
  • 9
  • 2
    If you want the sign to abut then number then specify it as a floating sign e.g. +,+++9.99 gives -0.99 if the value is -.99, +1.25 if the value is 1.25. – NicC Nov 22 '18 at 11:22