This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Converting alfanumeric string in numeric

Hi, in my lillte experience I have all the impossible cases of the word

I have a sequential file where is stored numbers in alfanumeric type ( examples "123,45"  "10,00"   "35780,00" "450" )  I need to convert them in numeric variable. How can I do it ?

I use a variable declared PIC X(16)  to store each field: If I see it on dislay is correct ( after I have removed the " )

I have tried with all the know-how of my mind but I have results very disastrous ( examples 123459,00  10009,00 )

I hope the problem is the sign that I declare in the recipient variable [ 77 W-NUMERO pic S9(11)v99 ]

Someone can suggest me a method ???

Many thanks to all you

  • Verified Answer

    0

    Redefine the X(16) with Z(16):

    01   my-num-alpha                           pic x(16).
    01   my-num-edit redefines my-num-alpha     pic Z(16).

    Then, after you have removed the quote (") characters:

                MOVE my-num-edit TO numeric-item.

    The MOVE statement will de-edit the number using the rules of ACCEPT CONVERT.

    RM/COBOL Language Reference Manual, Second Edition
    Page 352

  • 0  

    If Tom's excellent suggestion does not work for you, can you post some example code of what you're trying to accomplish? Perhaps you're close and it just needs some small changes.

  • 0 in reply to 

    Wow ! It works very well. I wasted a lot of time ......................   very grateful !

  • 0 in reply to 

    Perhaps you have someone sending comma-separated data to you in a sequential file.  You can pair the numeric de-edit with the following example code I wrote 20 years ago.  

          identification division.
           program-id.  unstring-fields.
           data division.
           working-storage section.
           01  binary.
               02  I               PIC S9(4).
               02  J               PIC S9(4).
           01  the-delimiter       PIC X(3).             
           01  INPUT-FIELD  PIC X(123) VALUE 
               'Bill Smith,"444-55-6666",999999999,1234567,"$1,060.46 "'.
           78  INPUT-FIELD-SIZE value LENGTH OF INPUT-FIELD.
    
           01  .
               02  OCCURS 40.
                   03  FIELD-HOLDER PIC X(50).
    03 FIELD-EDIT REDEFINES FIELD-HOLDER
    PIX Z(18)B(32). 03 FIELD-LENGTH PIC 9(4). procedure division. a. MOVE 1 to I. MOVE 1 TO J. PERFORM UNTIL I > INPUT-FIELD-SIZE INSPECT INPUT-FIELD (I:) TALLYING I FOR LEADING SPACE IF I NOT > INPUT-FIELD-SIZE EVALUATE INPUT-FIELD (I:1) WHEN '"' ADD 1 TO I IF I NOT > INPUT-FIELD-SIZE UNSTRING INPUT-FIELD DELIMITED BY '",' OR '"' INTO FIELD-HOLDER (J) DELIMITER IN THE-DELIMITER COUNT IN FIELD-LENGTH (J) POINTER I END-UNSTRING ADD 1 to J END-IF WHEN OTHER UNSTRING INPUT-FIELD DELIMITED BY "," INTO FIELD-HOLDER (J) DELIMITER IN THE-DELIMITER COUNT IN FIELD-LENGTH (J) POINTER I END-UNSTRING ADD 1 to J END-EVALUATE END-IF END-PERFORM. SUBTRACT 1 FROM J. stop run.

    Note that this code provides the skeleton of scanning comma-delimited records, including detecting fields that are enclosed in quotation marks (").  Subsequent conversion of numeric values, embedded quotation marks, and other anomalies you might find in 'real life' are not addressed.

  • 0 in reply to 
    another great idea ! Many thanks to you