×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Converting alphanumeric field to numeric
2

Converting alphanumeric field to numeric

Converting alphanumeric field to numeric

(OP)
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

RE: Converting alphanumeric field to numeric

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

RE: Converting alphanumeric field to numeric

You can also try this if you can change the field description:

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.
 

RE: Converting alphanumeric field to numeric

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

RE: Converting alphanumeric field to numeric

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 "ab123lf456".
        ..
        Call "STRTONUM" Using Alpha-Field.

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

Hope it helps.

RE: Converting alphanumeric field to numeric

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

COMPUTE TOTAL-AMT-WS = FUNCTION NUMVAL(INPUT-AMT)

Note well, however, that if the field is not numeric the program with abend with a datatype error.
 

RE: Converting alphanumeric field to numeric

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

RE: Converting alphanumeric field to numeric

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 "number" is now negative.

RE: Converting alphanumeric field to numeric

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

RE: Converting alphanumeric field to numeric

MIKELEIBO,

Simplicity is GOOD but unfortunatelly not always an option.
You state "If it contains alpha characters - it ain't numeric, was created inproperly as the result of poor design or poor data entry controls". 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.

RE: Converting alphanumeric field to numeric

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

RE: Converting alphanumeric field to numeric

Dimandja,

Thanks for the advice. You're 100% right

Theophilos.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close