Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Check if string is numeric

Former Member
0 Likes
2,858

FORM IsNumeric.

DATA:   numeric(10) TYPE C VALUE '0123456789 ',
             account(10) TYPE C VALUE '600',
             rc TYPE C.

  IF account CO numeric.
      rc = ''.
  ELSE.
      rc = 'X'.
  ENDIF.
ENDFORM.

The result is  rc = 'X'

Where I'm wrong?

Regards,

Mioa

1 ACCEPTED SOLUTION
Read only

arindam_m
Active Contributor
0 Likes
2,522

Hi,

your variable account(10) TYPE C VALUE '600'. Is of length 10 so account is actually '       600' i.e. with 7 spaces in the value. but your string '0123456789 ' does not have space as the length is 10 so the space is truncated, so numeric = '0123456789' all total 10 chars the space is missing due to length. If numeric is of length 11 it will work.

Cheers,

Arindam

6 REPLIES 6
Read only

arindam_m
Active Contributor
0 Likes
2,523

Hi,

your variable account(10) TYPE C VALUE '600'. Is of length 10 so account is actually '       600' i.e. with 7 spaces in the value. but your string '0123456789 ' does not have space as the length is 10 so the space is truncated, so numeric = '0123456789' all total 10 chars the space is missing due to length. If numeric is of length 11 it will work.

Cheers,

Arindam

Read only

Former Member
0 Likes
2,522

Try this code .

DATA:   numeric(10) TYPE C VALUE '0123456789 ',

             account(3) TYPE C VALUE '600',

             rc TYPE C.


  IF account CO numeric.

      rc = ''.

  ELSE.

      rc = 'X'.

  ENDIF.

ENDFORM.

Read only

Former Member
0 Likes
2,522

Mio,

If the value for account was '0000000600' your code will work fine. Try as per below:

DATA:   numeric(10) TYPE C VALUE '0123456789 ',

             account(10) TYPE C VALUE '600',

             len type char10,

             rc TYPE C.

  condense account.

   len = strlen ( account ).

 

  IF account+0(len) CO numeric.

      rc = ''.

  ELSE.

      rc = 'X'.

  ENDIF.

This should solve your issue.

Thanks,

Vikram.M

Read only

Former Member
0 Likes
2,522

Hi Mio,

Check if this below function module helps you.

NUMERIC_CHECK

Regards,

Santanu.

Read only

Former Member
0 Likes
2,522

Hi,

you can see this example:

   REPORT  z_check_string.


DATA:  account(10) TYPE c VALUE '2001',
             rc TYPE c,
             lung TYPE i,
             pos TYPE i,
             char(1).


lung = strlen( account ).
DO lung TIMES.
  char = account+pos(1).
  IF char CO '0123456789'.

*here if you want you can move char into others flow..............
  ELSE.
    rc = 'X'.
    EXIT.
  ENDIF.
  pos = pos + 1.
ENDDO.

WRITE rc.

I hope that this example helped you and solve your issue.

Regards

Ivan

Read only

custodio_deoliveira
Active Contributor
0 Likes
2,522

To make use of more modern ABAP I'd rather write:

DATA:   numeric(11) TYPE C VALUE '0123456789 ',   "as suggested by arindam

             account(10) TYPE C VALUE '600',

             rc TYPE C.


rc = boolc( account CO numeric ).

boolc returns a boolean for the given expression.

Cheers,

Custodio