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 numeric value

Former Member
85,191

Hello,

please tell me a possibility how i can check if the value of a variable is numeric.

Thank you!

Ronny

1 ACCEPTED SOLUTION
Read only

Former Member
31,229

Hello,

Use the FM NUMERIC_CHECK.

Reward if helps.

19 REPLIES 19
Read only

Former Member
0 Likes
31,226

Hi,

make a constant

c_numeric type string value ' .,0123456789'.

if value cn c_numeric.

not numeric

else.

numeric

endif.

grtz

Koen

Read only

0 Likes
31,226

thanks Labie koen, I had the same doubt your answer clarified my doubt.

Read only

Former Member
31,230

Hello,

Use the FM NUMERIC_CHECK.

Reward if helps.

Read only

Former Member
31,226

try this

if var CO '0123456789'.
  *var is numeric.
else.
 *non numeric.
endif.

Read only

Former Member
0 Likes
31,226

Hi ronald,

1. This is one way

2. we can use CO (contains only relational operator)

3. just copy paste

4.

NOTE :

'0123456789. ' (There is a SPACE after .DOT)

report abc.

PARAMETERS : A(15) TYPE C.

<b>if A CO '0123456789. '.</b>

WRITE 😕 'NUMBER'.

ELSE.

WRITE 😕 'NOT A Number'.

ENDIF.

regards,

amit m.

Read only

Former Member
0 Likes
31,226

data : v_char(10) type c value '0123456789'.

if v_number CO v_char.

its a number

else.

its not a number

endif.

Read only

0 Likes
31,226

thank you guys!

Read only

RichHeilman
Developer Advocate
Developer Advocate
31,226

Yep, try this.




report zrich_0001.

data: str type string.

str = '15263459846'.

if str co '1234567890'.
  write:/ 'This is numeric'.
else.
  write:/ 'This is not numeric'.
endif.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
31,226

IF lnum CO '0123456789'

numeric.

endif.

Read only

Laxmana_Appana_
Active Contributor
0 Likes
31,226

Hi,

check this :

IF x_data <b>CN</b> '12345 67890'.
   write : ' Not a number'.
ELSE.
   write : 'Number'.
ENDIF.

Regards

Appana

Read only

31,226

Also, if your field is a fix length and your value does not take up the entire field, you should check the length and check only that part of the field.



report zrich_0001.

data: c(20) type c value '123456'.
data: length type i.

length = strlen( c ).

if c+0(length) co '1234567890'.
  write:/ 'It is a numeric'.
else.
  write:/ 'It is not a numeric'.
endif.

Regards,

Rich Heilman

Read only

0 Likes
31,226

Please make sure to award points for these helpful answers and mark your post as solved when solved completely. THanks and welcome to SDN.

Regards,

Rich Heilman

Read only

0 Likes
31,226

Hi,

I want to check for only numeric value for material i.e. if my material contains only numbers then i want to set indicator to Y. I have written following code in field level routine.

DATA: lc_num(12) TYPE c VALUE '0123456789'.

DATA: lv_string TYPE c.

lv_string = SOURCE_FIELDS-MATERIAL .

   IF lv_string CO lc_num.
      RESULT = 'Y'.
   ENDIF.

The problem is when i input '1234' value it sets indicator to Y which is correct,when i input value 'K1234' it sets indicator to blank which is right but when i input value '1234K' it sets indicator to Y which is incorrect as value is not numeric.

Please help.....

Read only

0 Likes
31,226

Hi Anuradha,

Use 'NUMERIC_CHECK' Function Module to check value is char or int.

Regards

Lingaraj

Read only

0 Likes
31,226

Hi Lingaraj,

Thanks for your suggestion. I have used NUMERIC CHECK function module and it's working fine.

Regards,

Anuradha

Read only

Former Member
31,226

Hello Ronald,

you can try FM - CATS_NUMERIC_INPUT_CHECK  [It helps me]

This FM fetches the user's default setting on decimal place.

and then accordingly, classify if entered data is numeric or not.

Infact, this FM is internally used by SAP standard program especially to check quantity value liek ekpo-menge.

this FM is sophisticated enough that it wont allow any alphabets except digits and literals like comma and decimal.

At least it will help one, who comes across this page.

FYI - NUMERIC CHECK [is not good enough to distinguish NUMC and CHAR if u enter comma/deccimal]

Thank you.

best regards,

Rahul Agarwal

Read only

0 Likes
31,226

thanks, this helped me.

Read only

mbukli
Discoverer
0 Likes
31,226

Hello there,

How do we do it using the Numeric_Check function.

Read only

kevin_albrecht
Explorer
0 Likes
31,226

Hi everyone,

I know this is an old thread, but maybe someone will come across it anyway. You could try something like this:

DATA(value) = '-20233.44'.

IF value CO '0123456789.,-'.
  TRY.
      DATA(test) = CONV decfloat16( value ).
      DATA(is_numeric) = abap_true.
    CATCH cx_sy_conversion_no_number.
      is_numeric = abap_false.
  ENDTRY.
ENDIF.

However, if you'd like date and time values not to be considered as numeric values, the logic above must be extended accordingly.