0

How to check if a number have integer representation

number can be a float or double

2 Answers 2

4

In Lua 5.3+, use math.tointeger:

math.tointeger (x)

If the value x is convertible to an integer, returns that integer. Otherwise, returns nil.

Sign up to request clarification or add additional context in comments.

1 Comment

dont know that really exist because google search give no result about what im asking so i make Q&A about this so people dont need to figure it out by themself
0

Maybe check numbers for dot to see if it is a float or integer?

Then check this out...

dotchk=function(...);
local args={...}
assert(args[1],'Need argument')
local _,b=tostring(args[1]):gsub('%.','') -- Check for dot

if b>0 then
 print('floating point found in:',args[1])
 return true
else
 print('No dot in:',args[1])
 return false
end

end

Three examples...

>dotchk(math.pi)
floating point found in:    3.1415926535898
true
>dotchk(math.maxinteger)
No dot in:  9223372036854775807
false
>dotchk(math.mininteger)
No dot in:  -9223372036854775808
false

3 Comments

How about print(1.5e2) and dotchk("1.2.3")?
dotchk(1.5e2) works directly for "1.2.3" i suggest under the first assert a second one: assert(type(args[1])=='number','Give me a number - Please') -- No number - ;-)
the question doesnt ask for checking whether is it floating number or not its asking for checking is it has integer representation like 1.0, 34, 2.0, etc

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.