math.modf

Shared

Returns the integer and fractional parts of a number


Syntax

local integer, fraction = math.modf(
    value
)

Parameters

TypeNameDescription
numbervalueNumber to split into integer and fractional parts

Returns

TypeNameDescription
intintegerInteger part of value
numberfractionFractional part of value (same sign as value)

Examples

Split a positive float
local integer, fraction = math.modf(4.75)

engine.print("info", integer, fraction) -- 4  0.75
Split a negative float
local integer, fraction = math.modf(-3.25)

engine.print("info", integer, fraction) -- -3  -0.25
Split an exact integer
local integer, fraction = math.modf(10.0)

engine.print("info", integer, fraction) -- 10  0.0
Get only the integer part
local integer = math.modf(9.99)

engine.print("info", integer) -- 9
Get only the fractional part
local _, fraction = math.modf(9.99)

engine.print("info", fraction) -- 0.99
Separate seconds and milliseconds
local elapsed = 3.456
local secs, ms = math.modf(elapsed)

engine.print("info", secs, math.floor(ms * 1000)) -- 3  456
Check if a number is a whole integer
local n = 5.0
local _, fraction = math.modf(n)

if fraction == 0 then
    engine.print("info", "Whole number")
end

On this page