How to check if type of a variable is float? Float isn't basic type of values in Lua, so function type() return only number.
2 Answers
All numbers in Lua are floating point1. You can tell is a number represents a number with non-zero fractional part by using math.modf.
1 Unless lua's code itself is compiled with a flag that instructs it to use integers instead of floating-point numbers, in which case you cannot use floating point numbers in your programs at all.
1 Comment
daurnimator
You can also check if a number has a fractional part with
n%1 == 0math.type() tells you if the number is a float or an integer. It's only available on Lua 5.3
See the Lua 5.3 Reference Manual of math functions https://www.lua.org/manual/5.3/manual.html#6.7
1 Comment
idbrii
However,
print(math.type(0.5 + 0.5)) prints "float" -- it's giving you the storage type which may not be what you want.