Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to determine if a number is an integer?

So, Yes, I know ho to make a function to do this. But, I am wondering if type() or typeof() have a string for integer that they return, just to make things simpler. So, is there one? Because if you do this:

local thingy = "str"
if typeof(thingy) == "string" then -- tells you if the string you are comparing is returnable, but integer isn't based on it, just want to double check with you guys.
	print("Oof")
end

Then it tells you if that is a value it can return, so you can see if you are doing things right for figuring out if it i a string, a number, but it doesn't do it for integer.

1 comment

0

Because, I am pretty sure that integer is something that type() or typeof() can return, but it doesn't say it can be returned by type(), and typeof() doesn't really give you a big description, so do you guys know all the returnable values aswell?

1 answer

✓ Accepted answer

typeof and type will return "number" which includes decimals...

To check for an integer you would do the following

if typeof(thing) == "number" and math.floor(type) == type then
    -- must be an integer
end

Im not sure if you want yo be using tonumber to attempt to convert the input to a number first but it will return nil if its unable to convert the input to a number. You would then do the same check as above.

I hope this helps. Please comment if you have any other questions about this code.

3 comments

0

So there is no way to use type to find an integer? Dang...

0

The concept of integer does not exist in Lua they are simply numbers as opposed to int, fload, double

0

Ok

This discussion is preserved as it was. New answers, comments, and votes are closed.