Codectionary / Developer documentation / Lua

Lua integral and fractional parts

math.modf splits a number into integral and fractional parts with the original sign. Its integral part truncates toward zero, unlike floor for negative inputs.

Syntax

local whole, fraction = math.modf(value)

Examples

Lua integral and fractional parts

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

local whole, fraction = math.modf(-3.75)
print(whole)
print(fraction)
print(math.floor(-3.75))
  1. Numeric output: -3, -0.75, then -4. Truncation and rounding downward have different results for negative values.

Best practices

  • Choose truncation or floor according to the intended rule. Binary floating-point fractions are not always exact.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading