How can I convert an alphanumeric and use it for calculation?

lamwaiman1988 picture lamwaiman1988 · Jul 27, 2011 · Viewed 29.1k times · Source

I will read a sequential file which include some string such as "79.85", "1000", "212.34".

I want to convert the alphanumeric into number in this format 00000.00 ?

I will need to add up these numbers and move it to a field in the format 0000000.00 .

I tried:

01 WS_AMOUNT_TXT                                PIC X(8).
01 WS_AMOUNT                                    PIC 9(5).9(2).

MOVE WS_AMOUNT_TXT(1:8) TO WS_AMOUNT(1:8).

What I got is unexpected, the string is just as same. It is left align and no leading zero display.

How can I made it right align and have leading zero?

EDIT: I tried the suggestion by NealB, and it sadly failed:

01 WS_AMOUNT_NUM                                PIC 9(5)V9(2).
01 WS_AMOUNT_DISPLAY                            PIC 9(5).9(2).
01 WS_AMOUNT_TXT                                PIC X(8).


        DISPLAY WS_AMOUNT_TXT

        COMPUTE WS_AMOUNT_NUM = FUNCTION NUMVAL (WS_AMOUNT_TXT)
        MOVE WS_AMOUNT_NUM TO WS_AMOUNT_DISPLAY

79.85 << this is what was displayed when I called DISPLAY WS_AMOUNT_TXT AND THEN IT CRASHED.

%COB-F-NUMVALARGINV, NUMVAL or NUMVAL-C argument invalid %TRACE-F-TRACEBACK, symbolic stack dump follows image module routine line rel PC abs PC DEC$COBRTL 0 000000000001F2B8 000000007C2F72B8 DEC$COBRTL 0 0000000000014764 000000007C2EC764 DEC$COBRTL 0 0000000000014C44 000000007C2ECC44 DAILY_SPLIT_REFUND_ADJ DAILY_SPLIT_REFUND_ADJ DAILY_SPLIT_REFUND_ADJ 121 00000000000003C4 00000000000303C4 DAILY_SPLIT_REFUND_ADJ 0 00000000000313A0 00000000000313A0 0 FFFFFFFF80271EF4 FFFFFFFF80271EF4

Answer

NealB picture NealB · Jul 27, 2011

Try using the intrinsic function NUMVAL to do the conversion. Something like...

    01 WS-AMOUNT-TEXT     PIC X(8).  
    01 WS-AMOUNT-NUM      PIC 9(5)V9(2).  
    01 WS-AMOUNT-DISPLAY  PIC 9(5).9(2).  

    COMPUTE WS-AMOUNT-NUM = FUNCTION NUMVAL (WS-AMOUNT-TEXT)  
    MOVE WS-AMOUNT-NUM TO WS-AMOUNT-DISPLAY  

NUMVAL converts the text representation of a number into a numeric type. Use the numeric data type: PIC 9(5)V9(2) in your calculations. Then use MOVE to convert the numeric result into a displayable amount with explicit decimal point.

Note: If you have a lot of calculations to perform, it might be best to use a PACKED-DECIMAL data type to improve efficiency.