is_numeric?

Posted by Katie Love on Tue 24 Feb 2009 03:17 AM — 3 posts, 18,338 views.

#0
Is there a lua equivalent to the php is_numeric()?

http://us2.php.net/is_numeric

I've been searching Google and these forums with no luck...

Any thoughts would be very helpful!

Thanks :-)
USA #1
if tonumber(x) then ...


tonumber() returns a number or nil if it can't convert.
Australia Forum Administrator #2
I agree with Willfa, that is an easy way.

However a bit depends on what you define as "numeric" in your application.

For example:


print (tonumber ("0xff"))  --> 255
print (tonumber ("1e6"))  --> 1000000


The value "0xff" is considered a numeric literal in Lua, and hence it converts. If you are really asking "how to test for 0 to 9 with an optional leading sign, you may be better off with a regular expression (ie. string.match).