math.tointeger
Shared
Converts a number to an integer if it has no fractional part, otherwise returns nil
Syntax
local result = math.tointeger(
value
)Parameters
| Type | Name | Description |
|---|---|---|
number | value | Number to attempt converting to an integer |
Returns
| Type | Name | Description |
|---|---|---|
int | nil | result | Converted result: • int — exact integer representation of value • nil — value has a fractional part or is not a number |
Examples
local result = math.tointeger(5.0)
engine.print("info", result) -- 5local result = math.tointeger(5.5)
engine.print("info", result) -- nillocal result = math.tointeger(-3.0)
engine.print("info", result) -- -3local result = math.tointeger("hello")
engine.print("info", result) -- nillocal val = 2 ^ 10
local int_val = math.tointeger(val)
if int_val then
engine.print("info", "Integer:", int_val) -- Integer: 1024
else
engine.print("info", "Not an exact integer")
endlocal index = 9 / 3
local int_index = math.tointeger(index)
if int_index then
engine.print("info", "Valid index:", int_index) -- Valid index: 3
end