Loading content...
Math
Common numeric helpers missing from Lua's stdlib — round, clamp, lerp, distance, random.
Common numeric helpers missing from Lua's stdlib — round, clamp, lerp, distance, random.
lib.math adds the numeric helpers Lua's standard library lacks. Shared (client + server).
| Function | Returns | Description |
|---|---|---|
round(value, decimals?) | number | Round to N decimals (default 0). |
clamp(value, min, max) | number | Constrain value between min and max. |
lerp(a, b, t) | number | Linear interpolation; t is not clamped. |
groupdigits(value, sep?) | string | Group digits for display (1234567 → 1,234,567). |
distance(a, b) | number | Euclidean distance between two vector3 (sugar for #(a - b)). |
randomFloat(min, max) | number | Random float in [min, max]. |
round(value, decimals?)numberclamp(value, min, max)numbervalue between min and max.lerp(a, b, t)numbert is not clamped.groupdigits(value, sep?)string1234567 → 1,234,567).distance(a, b)numbervector3 (sugar for #(a - b)).randomFloat(min, max)number[min, max].local price = lib.math.round(1234.5678, 2) -- 1234.57
local hp = lib.math.clamp(health, 0, 200)
local mid = lib.math.lerp(0, 100, 0.25) -- 25
local money = lib.math.groupdigits(1500000, ' ') -- '1 500 000'
local dist = lib.math.distance(GetEntityCoords(PlayerPedId()), target)clamp doesn't verify min <= max. lerp's t isn't bounded (passing 2 extrapolates past b). randomFloat uses the global math.random — not for crypto.