5

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.

1
  • 1
    If i remember correctly, all numbers are floating point in Lua. Integers are only floating point numbers without the decimals. Commented Aug 3, 2012 at 11:38

2 Answers 2

6

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.

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

1 Comment

You can also check if a number has a fractional part with n%1 == 0
4

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

However, print(math.type(0.5 + 0.5)) prints "float" -- it's giving you the storage type which may not be what you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.