mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 23:46:03 +01:00
feat(math): add math.round function (#603)
This commit is contained in:
@@ -197,4 +197,25 @@ function math.lerp(start, finish, duration)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Rounds a number to a whole number or to the specified number of decimal places.
|
||||||
|
---@param value number | string
|
||||||
|
---@param places? number | string
|
||||||
|
---@return number
|
||||||
|
function math.round(value, places)
|
||||||
|
if type(value) == 'string' then value = tonumber(value) end
|
||||||
|
if type(value) ~= 'number' then error('Value must be a number') end
|
||||||
|
|
||||||
|
if places then
|
||||||
|
if type(places) == 'string' then places = tonumber(places) end
|
||||||
|
if type(places) ~= 'number' then error('Places must be a number') end
|
||||||
|
|
||||||
|
if places > 0 then
|
||||||
|
local mult = 10 ^ (places or 0)
|
||||||
|
return math.floor(value * mult + 0.5) / mult
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return math.floor(value + 0.5)
|
||||||
|
end
|
||||||
|
|
||||||
return lib.math
|
return lib.math
|
||||||
|
|||||||
Reference in New Issue
Block a user