Codectionary / Developer documentation / Luau

Luau nearest-integer rounding

math.round rounds to the nearest integer and resolves halfway cases away from zero. That rule differs from math.floor(x + 0.5) for negative halfway values.

Syntax

local rounded = math.round(value)

Examples

Luau nearest-integer rounding

Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.

print(math.round(2.5))
print(math.round(-2.5))
local step = 0.25
local snapped = math.round(1.13 / step) * step
print(snapped)
  1. Output: 3, -3, 1.25. Scaling by a step before rounding snaps a value to a grid.

Best practices

  • Choose the rounding rule explicitly. Floating-point grid snapping does not provide exact decimal accounting.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading