Skip to Content
-2

Numeric value check

Oct 26, 2016 at 02:48 AM

260

avatar image
Former Member

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?

10 |10000 characters needed characters left characters exceeded
Former Member

We want that system treat this value as Error if customer input "-"(Negative sign only) or "."(Period only).

0

Now this is just getting confusing. "Only"? So now if someone enters "-1" it's OK but "-" is not OK?

0
Former Member

>>Jelena.

You are corecct. "-1" it's OK but "-" is not OK.
We will try to add to code like as Mr.Volker.

0
Former Member

Dear All

Thank you for many response.
I understand that SAP have not other function module which match our require.
So, I will programing like as Mr.Volker.

0
* Please Login or Register to Answer, Follow or Comment.

5 Answers

Matthew Billingham
Oct 26, 2016 at 05:07 AM
2

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

Share
10 |10000 characters needed characters left characters exceeded
Volker Binder Oct 26, 2016 at 06:43 AM
1

an easy way to check is:

TRY.

MOVE 'check_value' TO 'numeric variable'.

CATCH CX_SY_CONVERSION_NO_NUMBER.

MESSAGE ... 'no number'.

ENDTRY.

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Oct 26, 2016 at 07:01 AM
1

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.

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Oct 26, 2016 at 07:10 AM
1

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.

Share
10 |10000 characters needed characters left characters exceeded
Nic Teunckens Oct 26, 2016 at 08:02 AM
1

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.
Share
10 |10000 characters needed characters left characters exceeded