cancel
Showing results for 
Search instead for 
Did you mean: 

Function to check if string is a valid number

Former Member
0 Kudos

Hello Everyone,

I was wondering if there is any function in MII to check if the given string is a valid number . The closest that I can get is "number(string)" function which converts the the I/P string to a number or throws a exception if the I/P is not a valid number . I am looking for some which returns me 0 or 1 based on the I/P value .

(sth like "numbercheck(string,0,1)"; )

Thanks,

Naresh Agarwal,

Accepted Solutions (1)

Accepted Solutions (1)

HariCS23
Contributor
0 Kudos

RegEx can do this task in a very efficient way.

Try this in expression editor: match("1234","^[0-9]+$")

Thanks

Hari

Former Member
0 Kudos

Hello Naresh,

1.     There is no direct function in MII to check if the string is number or not. However, as Hari mentioned, you can use match() and pass a regex to check ur case.

For example, if you only have an integer, then Hari's regex pattern works very well

match("1234","^[0-9]+$") ==> 0, false- 1, true

If you have decimal notation too, then try using this pattern

match("14.0", "^([+-]?\\d*\\.?\\d*)$") ==> 0, false- 1, true

2.    The number(string) function, you tried would only throw exception if the entire string is a string say "abcd", if you pass "12abcd12", then the function would return you 12.

3.    The other method is to use a local variable of type integer/float/double and try assigning the value to it . If an invalid value like abcd is assigned to the variable, then a DataConversionException would be thrown. I guess you can catch(catch action block) this and apply your business logic if needed.

Regards,

Tufale Ashai.

Answers (3)

Answers (3)

former_member206575
Participant
0 Kudos

This message was moderated.

Former Member
0 Kudos

A simular discussion can be found here:

http://scn.sap.com/message/13551358

Hope this helps,

Philippe

Former Member
0 Kudos

Hi Naresh,

The function number() returns the actual number if a numeric value is passed to it and returns NA if a string is passed to it provided the value to be passed to this function should be passed without double quotes.

Further, to fulfill your requirement you can use stringif(number(VALUE) == "NA",0,1).

Hope this helps.

Regards,

Anuj