I'm creating a math application, the question answers are stored in a variable called answer, i need a way to check if the answer has a decimal point in it, so for example say if the question generated is 4 divided by 3, i want to be able to check if the answer will have a decimal in it. Thank you.
Asked
Active
Viewed 3,377 times
3 Answers
3
Check out the math standard library. One way of checking if a number is an integer would be using the floor function:
x == math.floor(x)
Of course, this assumes x is a number and not a string.

hugomg
- 68,213
- 24
- 160
- 246
-
Wow! Didn't realise it could be so easy!! Thank you! – curtyo Mar 19 '13 at 23:53
-
@lhf: The floor operation will work but the equality test will fail because it will be comparing a string to a number. – hugomg Mar 20 '13 at 03:18
-
You're right, of course. Sorry for the noite. I've removed that comment. – lhf Mar 20 '13 at 10:33
0
you can use the modulus operator
if x % 1 == 0 then
-- x does not have decimal point
else
-- x does have a decimal point
end
another option is modf
local integral, fractional = math.modf(x)
if fractional == 0 then
-- x does not have decimal point
else
-- x does have a decimal point
end

Suphi Kaner
- 59
- 7
0
it seems you need to check if a number is divisible by others, so just use modulus (if you don't know, modulus is the remainder of the division)
if N % D == 0 then
print("divisible")
else
print("not")
end
(this will print "divisible" if N is a multiple of D)

Leol22
- 3
- 4