Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting alphanumeric field to numeric 2

Status
Not open for further replies.

hugheskbh

Programmer
Dec 18, 2002
37
0
0
US
I need to take the values in a alphanumeric field and place in a numeric field. Is there a way to convert or how can I accomplish this in Cobol.

Thanks
 
Try:

01 alpha-field PIC X(4) VALUE "1234".
01 numeric-field PIC 9(4).

MOVE alpha-field(1:4) TO numeric-field(1:4).

Dimandja
 
You can also try this if you can change the field description:
Code:
01  alpha-field PIC X(4) VALUE "1234".
01  numeric-field REDEFINES
    alpha-field PIC 9(4).
Just make sure the A/N field doesn't contain alpha or special chars. That goes for Dimandja's solution too.

Regards, Jack.
 
If you move an alphanumeric field to a numeric field, this is VALID in '85 ANSI/ISO COBOL *if and only IF* the alphanumeric field contains an INTEGER value (signed or unsigned).

On the other hand, you can use the NumVal (or NumVal-C) intrinsic functions (with any compiler supporting the '89 ANSI/ISO amendment) to the "numeric" values of an alphnanumeric field that contains spaces, decimal points, comma, (or with NumVal-C) currency signs. Check your vendor's documentation to see if they support this - and if so what "restrictions" there are on the values in the alphanumeric field.

Bill Klein
 
If you mean that the alpha field contains numerals and chars mixed alltogether you can try the following subprogram (in RM/COBOL) :

@(#) *#################################*
@(#) * Convert ALPHANUMERIC to NUMERIC *
@(#) *#################################*
identification division.
program-id. STRTONUM.
author. Kanoutas Theophilos.
******************************************************************
* E N V I R O N M E N T D I V I S I O N *
******************************************************************
environment division.
configuration section.
source-computer. ibm-pc.
object-computer. ibm-pc.
special-names.
class digit is "0123456789"
decimal-point is comma.
******************************************************************
* D A T A D I V I S I O N *
******************************************************************
data division.
working-storage section.
1 arg-ok-flag pic x.
1 arg-desc binary.
2 arg-type pic 99.
2 arg-sz pic 9(8).
2 arg-digits pic 99.
2 arg-scale pic s99.
*----------------------------------------------------------------*
linkage section.
1 str-kodikos.
2 str-chr pic x occurs 1 indexed by i1 i2.
******************************************************************
* P R O C E D U R E D I V I S I O N *
******************************************************************
procedure division using str-kodikos.
procedure-division.
move 1 to arg-sz.
call "C$CARG" using arg-ok-flag str-kodikos(1:1) arg-desc.
set i1 to arg-sz.
set i2 to arg-sz.
perform until i1 < 1
if str-chr(i1) is digit
if i1 not = i2
move str-chr(i1) to str-chr(i2)
end-if
set i2 down by 1
end-if
set i1 down by 1
end-perform.
perform until i2 < 1
move zero to str-chr(i2)
set i2 down by 1
end-perform.
goback.
end program STRTONUM.



Call it like this:
01 Alpha-Field Pic x(..) Value &quot;ab123lf456&quot;.
..
Call &quot;STRTONUM&quot; Using Alpha-Field.

the result will be:
01 Alpha-Field Pic x(..) Value &quot;0000123456&quot;.

Hope it helps.
 
I don't know which version of COBOL you use, but I use the NUMVAL function:

[tt]COMPUTE TOTAL-AMT-WS = FUNCTION NUMVAL(INPUT-AMT)[/tt]

Note well, however, that if the field is not numeric the program with abend with a datatype error.
 
adding to the previous point i think the function numval will return a RC which we can trap..if any one knows better method to trap the Return code of intrinsic function please put it heere
 
Amazing how we complicate the simple. If it's numeric, it will past a numeric test and can be refined as numeric and used that way.

If it contains alpha characters - it ain't numeric, was created inproperly as the result of poor design or poor data entry controls, so attempts to treat it as numeric will result in unpredictable results. If the low order character is / or J-R, the &quot;number&quot; is now negative.
 
Right on MIKELEIBO,

Sometimes giving superfluous details will only complicate everything.

Simple questions should deserve equally simple answers. Most of the time that is all that was needed.

Dimandja
 
MIKELEIBO,

Simplicity is GOOD but unfortunatelly not always an option.
You state &quot;If it contains alpha characters - it ain't numeric, was created inproperly as the result of poor design or poor data entry controls&quot;. That is not always true. Data comming from input aren't always numeric or alpha for certain.

Let me give an example: An apllication provides to the customer the freedom to select what type/kind of product code he/she will use. Someone could select something like xxxxxxx or 999999 or even xx9999xxx9x9. How would you deal with a situation like this?

I think what hugheskbh asked for is a way to extracty only the numerals from a field, and not just check if it is numeric or not.

Theophilos.
 
Also right, theotyflos.

But, this means that care should be taken to clearly ask questions on this forum. The fact that a one liner question can be interpreted in so many different ways indicates that the question was probably not well worded.

My approach was to give a simple answer and encourage the questioner to pursue the issue and hopefully pinpoint the real answer they are after. Hopefully, with this technique, I won't lose them with a thorough and potentially disorienting answer.

Dimandja
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top