4

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.

curtyo
  • 43
  • 1
  • 4

3 Answers3

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
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
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