I want to check if a string is a number or not. For example if I have string1 = "10u" I want to use it to callculated some stuff but if string1 = "MET1" I just want to write it somewhere. All tips are appreciated
string1 = "10u"
string1 = "MET1"
you can try with numberp(cdfParseFloatString(string1)).
Thank you very much. That was exactly the function I was looking for.
Another option is to use:
However, in certain cases (e.g. pcell code), using cdfParseFloatString() may be preferred.
I tried aelNumber() before to test if its a float, but I always get Warnings when its not. The function mentioned before fits good for me. But I now discovered another problem: I not only need to check if its float or string but also if the string is an integer. Any suggestions for this? My main goal is to create CDF parameter variation of different cells. For this I read an check the parameters type and then decide how to variy them.
Thanks for you replies
I did originally think that maybe using atoi() would be the best approach, but since atoi("5u") returns 5, maybe that's not the best option. You might need to just do: pcreMatchp("^\\d+$" val)&&atoi(val)
Ok I used it and it seems to do what I need. I now check first with numberp(cdfParseFloatString(val) if val is a number, then check if val is integer or not with pcreMatchp("^\\d+$" val)&&atoi(val). Thank you all for your help!
By the way, you only really need to use the &&atoi(val) part if you want to return it as an integer; if you're just checking, then the pcreMatchp() would be sufficient.
I also thought that since atoi(string) returns nil when string is not an integer, that this would be sufficient. I wanna test if its an integer and if so also do some calculations with it. My calcultaions are depending on the number type of the string. So I use different values for a float then for the iteger number. Since I check with numberp(cdfParseFloatString(string)) if string is a number it is then sufficient to use atoi(string) to check if string is an integer. If not so it must be a float. Is this right or do I miss something here?
Ok its not working only with atoi(). I need the pcreMatchp()&&atoi() also. Then it does everthing I want.
FranzD said:Ok its not working only with atoi(). I need the pcreMatchp()&&atoi() also. Then it does everthing I want.
The problem is that numberp(cdfParseFloatString(string)) and atoi(string) would both return true (at least, non-nil) for both "5u" and "5". So it doesn't help to distinguish between the two. If atoi() had returned nil if there were any non-digit characters in the string, it would have been fine, but it doesn't, so that isn't a solution... hence the pcreMatchp suggestion.