Skip to Content
avatar image
-2
Former Member

Numeric value check

Hi all,

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

But if I input a value "-"(Negative sign only) the system treats this value as Numeric.We find "." also.

How to solve this without creating original function module?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Oct 26, 2016 at 05:07 AM

    Clearly the function module is not fit for purpose. So you'll have to write some custom code.

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 26, 2016 at 06:43 AM

    an easy way to check is:

    TRY.

    MOVE 'check_value' TO 'numeric variable'.

    CATCH CX_SY_CONVERSION_NO_NUMBER.

    MESSAGE ... 'no number'.

    ENDTRY.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 26, 2016 at 07:01 AM

    Hi Takashi,

    This may help you.

    CONSTANTS: lc_sy_numbers TYPE string VALUE '0123456789'.
    IF iv_string CN lc_sy_numbers.
    "not numeric
    ELSE.
    "numeric
    ENDIF.

    with regards,

    Pramod Kumar Mandal.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 26, 2016 at 07:10 AM

    Dear Takashi Watanabe,

    I don't think Function Module making mistake, maybe its your import/export parameter.

    Let me know you are receiving import parameter?. FM module return parameter(for us its import parameter) if given data is numeric('-200' or 200.00 or 200.0).

    Else it will not return value to return parameter. Then we decide that value is not numeric.

    Ex:

    REPORT ztest_test.
    DATA: lv_output(10) TYPE c,
    lv_input(10) TYPE c.
    *lv_input = '1.000'.

    *lv_input = '-1000'.

    lv_input = 'abcd'.

    CALL FUNCTION 'CATS_NUMERIC_INPUT_CHECK'
    EXPORTING
    input = lv_input
    INTERNAL = 'X'
    IMPORTING
    OUTPUT = lv_output
    EXCEPTIONS
    NO_NUMERIC = 1
    OTHERS = 2.
    IF sy-subrc <> 0.
    * Implement suitable error handling here
    ENDIF.

    WRITE: lv_output.

    You can try this code.

    1. uncomment lv_input ='1.000' then see output as 1.000

    2.uncomment lv_input = '-1000' then see output as 1000-

    3 uncomment lv_input = 'abcd' then you will not see any output, since lv_output has no value.

    Hope you understand.

    Thanks,

    Sivaraj Sadasivam.

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 26, 2016 at 08:02 AM

    Hello Takashi

    You could use a small Regex-check?

    Something along the lines of :

    DATA(lv_text) = '-'.
    
    TRY.
    
        DATA(lo_matcher) = cl_abap_matcher=>create(
              pattern = `([0-9]*)`
              text    = lv_text ).
    
        DATA(lv_matches_regex) = lo_matcher->match( ).
    
      CATCH cx_sy_matcher INTO DATA(lo_excp).
        DATA(lv_msg) = lo_excp->get_text( ).
    ENDTRY.
    Add comment
    10|10000 characters needed characters exceeded