How To Find If A String Is Also A Number?

A script I’m writing needs to check if the name of an items happens to be a number, using typeof().

However, it always returns “string”, because, for example, “5” is a string, while 5 is a number.

How do I check if it’s also a number, so that my script works?

1 Like

Use tonumber. It returns nil if its argument cannot be casted to a number.

print(tonumber("5") == 5) -- true
print(tonumber("five")) -- nil
3 Likes

if tonumber(x) then where x is a string.

  function IsNumber(n)
          if tonumber(n) then
       return true
  else
       return false
 end
 end

 print(IsNumber("3")) --> true
 print(IsNumber("three")) -- false
2 Likes