IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

What's the best way for convert char to numeric


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sungang77

New User


Joined: 03 Nov 2005
Posts: 46
Location: Shanghai, China

PostPosted: Sun Jul 23, 2006 3:57 pm
Reply with quote

If BMS map has a field, length is 8.
We don't know what the user will input.
Then we get the variable of this field in CICS program.
for example: the variable we input is(b stand for blank, s stand for space): "b76bs85s", we want check if we can convert it to '7685'.
What's the best way to judge if it's a legal numeric and convert it to a numeric?
Great thanks!!!!!
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Tue Jul 25, 2006 1:47 am
Reply with quote

You can try this code:

Code:

WORKING-STORAGE SECTION.                   
                                           
01  WS-INPUT            PIC X(8).         
01  WS-NBR              PIC 9(8).         
01  SUB1                PIC S9(5)  COMP-3.
01  SUB2                PIC S9(5)  COMP-3.
01  LEN2                PIC S9(5)  COMP-3.

PROCEDURE DIVISION.                               
                                                   
    MOVE LENGTH OF WS-NBR TO LEN2.                 
                                                   
    MOVE 0     TO WS-NBR.                         
    MOVE ' 76  85 '       TO WS-INPUT.             
    PERFORM P2000-CONVERT THRU P2000-EXIT.         
                                                   
    MOVE 0     TO WS-NBR.                         
    MOVE 'S76  85 '       TO WS-INPUT.             
    PERFORM P2000-CONVERT THRU P2000-EXIT.         
                                                   
    GOBACK.                                       

P2000-CONVERT.                                                 
                                                               
    DISPLAY 'WS-INPUT >' WS-INPUT '<'.                         
                                                               
    MOVE LEN2       TO SUB2.                                   
                                                               
    PERFORM                                                     
      VARYING SUB1 FROM LENGTH OF WS-INPUT BY -1               
      UNTIL SUB1 < 1                                           
        IF WS-INPUT(SUB1:1) NUMERIC                             
        THEN                                                   
            MOVE WS-INPUT(SUB1:1) TO WS-NBR(SUB2:1)             
            SUBTRACT 1 FROM SUB2                               
        ELSE                                                   
            IF WS-INPUT(SUB1:1) = ' '                           
            THEN                                               
                CONTINUE                                       
            ELSE                                               
                DISPLAY 'WS-INPUT >' WS-INPUT '< NON NUMERIC'   
            END-IF                                             
        END-IF                                                 
    END-PERFORM.                                               
                                                               
    DISPLAY 'WS-NBR >' WS-NBR '< FROM WS-INPUT >' WS-INPUT '<'.
    DISPLAY ' '.                                               
                                                               
P2000-EXIT.                                                     
    EXIT.                                                       


results of the execution of this program

Code:

                                                     
WS-INPUT > 76  85 <                                 
WS-NBR >00007685< FROM WS-INPUT > 76  85 <           
                                                     
WS-INPUT >S76  85 <                                 
WS-INPUT >S76  85 < NON NUMERIC                     
WS-NBR >00007685< FROM WS-INPUT >S76  85 <           
                                                     


Dave
Back to top
View user's profile Send private message
mainframemouli

New User


Joined: 01 Mar 2005
Posts: 52
Location: Mysore

PostPosted: Tue Jul 25, 2006 11:53 am
Reply with quote

david ,
your answer is specific to the example the author has given .
I think the author needs generalized solution.

All your cics programs should have the normalization step to handle this situation.

try checking out NUMVAL and NUMVAL-C functions of cobol...i am not sure.
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Tue Jul 25, 2006 5:04 pm
Reply with quote

NUMVAL and NUMVAL-C both abend if the input value is not numeric.

My intention was not to code sungang77's program for him. My assumption is that he's intelligent enough to use the code example as a base, though I?m not sure what input condition will not work.

Dave
Back to top
View user's profile Send private message
sungang77

New User


Joined: 03 Nov 2005
Posts: 46
Location: Shanghai, China

PostPosted: Fri Jul 28, 2006 9:08 pm
Reply with quote

Thanks, Dave, and others.
I am sorry I do not see your answer until today because I am busy these days.
I know what you mean. It must work.
I forgot to tell you about y assumption is user can input anything.
If there's none numeric(not include space) in user's input, the repeat part should break as soon as program detect it, how can we improve the code?
Another thing, can we delete the space before we perform the repeat part?

P.S: I haven't use 'inspect' in cobol before, can it used in such situation?
Back to top
View user's profile Send private message
vivek_mvs

New User


Joined: 25 Apr 2006
Posts: 2
Location: United States

PostPosted: Fri Jul 28, 2006 9:36 pm
Reply with quote

Hi,

I think you can try this is CICS

Code:
EXEC CICS BIF DEEDIT
     FIELD(CONTG)
     LENGTH(9)
END-EXEC

Results

CONTG before execution CONTG after execution
Original value Returned value
14-6704/B 00146704B
$25.68 000002568
It extracts all the numer values and put it back in the variable

This removes all characters other than digits from CONTG, a 9-byte field, and returns the edited result in that field to the application program.Note that a decimal point is an EBCDIC special character and as such is removed.

Thanks
Vivek
Back to top
View user's profile Send private message
sungang77

New User


Joined: 03 Nov 2005
Posts: 46
Location: Shanghai, China

PostPosted: Mon Jul 31, 2006 7:51 pm
Reply with quote

Thanks so much, Vivek.
I have a question. As you refered, "This removes all characters other than digits from CONTG", why in the first example, the character "B" is still in the returned value?
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts Need to convert date format DFSORT/ICETOOL 20
No new posts Exclude rows with > than x occurre... DFSORT/ICETOOL 6
No new posts Keep leading zero(s) after convert fl... SYNCSORT 7
Search our Forums:

Back to Top