Application Development 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: 

Numeric value check

Former Member
0 Kudos

Hi all,

I am using  NUMERIC_CHECK to check if the value is numeric or not.

But if I input a value like 122.22 the system treats this value as Character.

I also tried another fm  CATS_NUMERIC_INPUT_CHECK with the same value, in the fm it gives me the same value in return but if i apply this fm in my program then it gives runtime error saying 'Unable to interpret . as number'

How to solve this?

any example.???

6 REPLIES 6

Former Member
0 Kudos

You can catch the runtime error to decide whether it is a number or not. Something like this.

  1. DATA str TYPE string.
  2. DATA num TYPE p LENGTH 15 DECIMALS 2.
  3. DATA is_number TYPE boolean.
  4. str = '122.22'.
  5. TRY .
  6.     num = str.
  7.     is_number = 'X'.
  8.   CATCH cx_sy_conversion_no_number.
  9.     is_number = ' '.
  10. ENDTRY.
  11. WRITE😕 is_number.

/.

Private_Member_14913
Contributor
0 Kudos

Hello Yash,

NUMBER_CHECK & CATS_NUMERIC_INPUT_CHECK both FM are trying to check number which are '0123456789 '.

In your example '122.22' dot after 122 system is treating as character. It is returning as Character.

You can use either of below approach.

  • DESCRIBE FIELD FLD TYPE T.
  • DESCRIBE FIELD FLD DECIMALS N.
  • PARAMETERS: P_DEC(10).

              IF P_DEC CN '.123567890 '.

                 MESSAGE E307 WITH 'ENTER CORRECT VALUE'.

              ENDIF.

  • FIELD-SYMBOLS <OFFSET> TYPE ANY.

        NB = STRLEN( W_FIELD ).

         DO NB TIMES.

                W_INDEX = SY-INDEX - 1.

                ASSIGN W_INDEX TO <OFFSET>.

                IF W_FIELD+<OFFSET>(1) CO '0123456789.'.

                else.

                exit.

                ENDIF.

         ENDDO.

Former Member
0 Kudos

Hi Yash,

As I understood , you want to check numeric with decimal also

Try this,


DATA : input(200) TYPE c,invalid TYPE c, valid TYPE c.


*  Only dot then also invalid

IF input CO '.'.

   invalid = 'X'.

* Contain otherthen numbers + dot then invalid

ELSEIF input CN '0123456789.'.

   invalid = 'X'.

ELSE.

   valid = 'X'.

ENDIF.


Keep this code in perform or function module check numeric

Thanks & Regards,

Arun



Former Member
0 Kudos

Hi,

I'd also use the CO operator: "if lv_string CO '123456789.'."

all the best,

Kieran

Former Member
0 Kudos

I am using module pool.

loop at it_weight INTO wa_weight.

       if wa_weight co '0123456789.'.

         st_REF-Zgross_WEIGHT = wa_weight-in.

       ENDIF.

     ENDLOOP.


after the loop on it_weight I have checked the condition. though it is not going in the if condition.

this is my code. I am getting value in wa_weight from clipboard.

0 Kudos

Hi,

May be spaces are present in wa_weight. If yes condense the wa_weight.

Thanks & Regards,

Arun