math.type

Shared

Returns the numeric subtype of a value as a string, or false if the value is not a number


Syntax

local result = math.type(
    value
)

Parameters

TypeNameDescription
anyvalueValue to check the numeric subtype of

Returns

TypeNameDescription
string | boolresultNumeric subtype of value:
"integer" — value is an integer
"float" — value is a float
false — value is not a number

Examples

Integer literal returns 'integer'
local result = math.type(1)

engine.print("info", result) -- 'integer'
Float literal returns 'float'
local result = math.type(1.0)

engine.print("info", result) -- 'float'
Non-number returns false
local result = math.type("hello")

engine.print("info", result) -- false
Result of integer division is integer
local result = math.type(10 // 3)

engine.print("info", result) -- 'integer'
Result of float division is float
local result = math.type(10 / 3)

engine.print("info", result) -- 'float'
Distinguish integer from float with the same value
engine.print("info", math.type(1))   -- 'integer'
engine.print("info", math.type(1.0)) -- 'float'
Branch logic by numeric subtype
local values = {1, 2.5, 3, 4.0, "hi"}

for _, v in ipairs(values) do
    local subtype = math.type(v)
    if subtype == "integer" then
        engine.print("info", v, "is an integer")
    elseif subtype == "float" then
        engine.print("info", v, "is a float")
    else
        engine.print("info", v, "is not a number")
    end
end

On this page