Convert alphanumeric(with special char) to numeric



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Convert alphanumeric(with special char) to numeric

Postby ddash1988 » Mon Aug 12, 2013 11:50 am

Hello All,

I need to check if an user has changed the amount field in CICS.
Basically I will be checking the received map(the amount field which ix X(14) and is in the format "_ _ _,_ _ _,_ _ _._ _" (11 digits, 2 commas and one dot)
The user can put the value (11 digits or less, as per the amount) with or without the comma and dot. whatever he gives, i have to convert it to a numeric field and update it in DB2.
Can anyone please tell me how to convert that input(alphanumeric) to numeric ? I tried to use NumVal and NumVal-C function, but somehow it is not working.
User input can be -
1000.00
500,000,000.00
500000000.00
500,000.00
ddash1988
 
Posts: 4
Joined: Fri Nov 25, 2011 6:30 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Convert alphanumeric(with special char) to numeric

 

Re: Convert alphanumeric(with special char) to numeric

Postby BillyBoyo » Mon Aug 12, 2013 2:39 pm

NUMVAL/NUMVAL-C only work with valid data. Not so hot for a CICS application if you can get blanks into the field. Can you? What about non-numerics (other than "," and ".")?
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Convert alphanumeric(with special char) to numeric

Postby ddash1988 » Mon Aug 12, 2013 3:14 pm

Hi Billy,

I will have to put a check somehow, as user can only input 0-9 and (comma & dot). No other character.
Just thought of an approach - hope it works

first i take data from screen x(14) and put it into a group var. then check each one in the grp variable for numeric, comma and dot. then transfer the numerics to another group variable. the use the 2nd grp variable to compute a field using numval. then finally use the computed field to update the table

eg.

03 MAXCPI PIC X(14).
10 WS-MAX-NUM PIC 9(9)V99.
05 WS-NUM-R1 PIC X(14) JUSTIFIED RIGHT.
05 WS-NUM-R2.
15 WS-NUM-R21 PIC X(3).
15 WS-NUM-R22 PIC X(1).
15 WS-NUM-R23 PIC X(3).
15 WS-NUM-R24 PIC X(1).
15 WS-NUM-R25 PIC X(3).
15 WS-NUM-R26 PIC X(1).
15 WS-NUM-R27 PIC X(2).

05 WS-NUM-R3.
15 WS-NUM-R31 PIC 9(3).
15 WS-NUM-R33 PIC 9(3).
15 WS-NUM-R35 PIC 9(3).
15 WS-NUM-R36 PIC X(1).
15 WS-NUM-R37 PIC 9(2).


MOVE MAXCPI(map input) TO WS-NUM-R1
MOVE WS-NUM-R1 TO WS-NUM-R2
PERFORM 3000-ALPHA-TO-NUM
IF WS-NUMERIC
MOVE ZERO TO WS-MAX-NUM
COMPUTE WS-MAX-NUM
= FUNCTION NUMVAL
ELSE
MOVE 'ENTER IN VALID FORMAT ' TO SYSMSGO

use WS-MAX-NUM to update in table


3000-ALPHA-TO-NUM.
------------------------------------------------------------

INSPECT WS-NUM-R25 REPLACING LEADING SPACES BY ZEROS.
INSPECT WS-NUM-R23 REPLACING LEADING SPACES BY ZEROS.
INSPECT WS-NUM-R21 REPLACING LEADING SPACES BY ZEROS.
MOVE ZEROS TO WS-NUM-R37
WS-NUM-R33
WS-NUM-R35
WS-NUM-R31.

SET WS-NOT-NUMERIC TO TRUE
IF WS-NUM-R27 IS NUMERIC AND
WS-NUM-R26 = '.' AND
WS-NUM-R25 IS NUMERIC AND
(WS-NUM-R24 = ' ' OR WS-NUM-R24 =',') AND
WS-NUM-R23 IS NUMERIC AND
(WS-NUM-R22 = ' ' OR WS-NUM-R22 =',') AND
WS-NUM-R21 IS NUMERIC
MOVE WS-NUM-R27 TO WS-NUM-R37
MOVE WS-NUM-R25 TO WS-NUM-R35
MOVE WS-NUM-R23 TO WS-NUM-R33
MOVE WS-NUM-R21 TO WS-NUM-R31
MOVE WS-NUM-R26 TO WS-NUM-R36
SET WS-NUMERIC TO TRUE
END-IF.
ddash1988
 
Posts: 4
Joined: Fri Nov 25, 2011 6:30 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Convert alphanumeric(with special char) to numeric

Postby BillyBoyo » Mon Aug 12, 2013 3:37 pm

With only 0-9 comma and full-stop/period then NUMVAL should not be a problem. What is the problem you have with NUMVAL?

You should look at what JUST RIGHT is doing for you. With fields of the same length, the answer is nothing, so I assume you think it is doing something else.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Convert alphanumeric(with special char) to numeric

Postby ddash1988 » Mon Aug 12, 2013 4:35 pm

BillyBoyo wrote:With only 0-9 comma and full-stop/period then NUMVAL should not be a problem. What is the problem you have with NUMVAL?

You should look at what JUST RIGHT is doing for you. With fields of the same length, the answer is nothing, so I assume you think it is doing something else.


NUMVAL works fine. But if the user inputs any alphanumeric in the field, then it fails. I have to put a check for that. (Client says user is dumb and can give anything!!)

I have used just right at the amount value can be anything from 999,999,999.00 to 0.00

and he can enter anything. if its a number, i have to accept it!!
ddash1988
 
Posts: 4
Joined: Fri Nov 25, 2011 6:30 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Convert alphanumeric(with special char) to numeric

Postby BillyBoyo » Mon Aug 12, 2013 5:40 pm

I thought you'd answered my question and you probably thought I'd asked a different one.

Get CICS (or the field definition to CICS) to do the justification.

Take off the JUSTIFIED RIGHT and you'll get identical results. So JUST RIGHT is giving you nothing, but you expect that it is. So JUST RIGHT is not giving you what you expect.

Someone did a very poor job designing the input form. Can the input be 50,000,000 or are they forced to include the .00 on the end?

If the ".00" is optional, you have four diffent formats. There's also ".0" and what about just "." is that valid, or not? That can increase things as well.

Yes, changing the leading spaces to zero is useful. Use INSPECT to count commas and full-stops/periods. Make sure you do not blindly assume that "," and "," are OK. Is 50,00 valid? No, because you can't tell for certain what it should be.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post