From 2b6edb0ac366df93115e70dbe81303dc116d10b8 Mon Sep 17 00:00:00 2001 From: Idris Dose Date: Fri, 1 Oct 2021 20:58:13 +1000 Subject: [PATCH] feat (Functions/Math): Added Precision Rounding so that people can round to nearest decimal efficiently. --- client/functions.lua | 19 +++++++++++++++++++ server/functions.lua | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/client/functions.lua b/client/functions.lua index 4f30682..be02eb0 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -451,3 +451,22 @@ function QBCore.Functions.SetVehicleProperties(vehicle, props) end end end + +-- Extra Math Functions + +-- Math Rounding Credits: http://lua-users.org/wiki/SimpleRound +function QBCore.Functions.Math.Sign(v) + return (v >= 0 and 1) or -1 +end + +-- Math Rounding Credits: http://lua-users.org/wiki/SimpleRound +-- Usage: +-- QBCore.Functions.Math.Round(119.68, 0.01) = 119.68 +-- QBCore.Functions.Math.Round(119.68, 0.1) = 119.7 +-- QBCore.Functions.Math.Round(119.68) = 120 +-- QBCore.Functions.Math.Round(119.68, 100) = 100 +-- QBCore.Functions.Math.Round(119.68, 1000) = 0 +function QBCore.Functions.Math.Round(v, bracket) + bracket = bracket or 1 + return QBCore.Functions.Math.Sign(v/bracket + QBCore.Functions.Math.Sign(v) * 0.5) * bracket +end diff --git a/server/functions.lua b/server/functions.lua index 16a052b..e866396 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -299,3 +299,22 @@ function QBCore.Functions.IsLicenseInUse(license) end return false end + +-- Extra Math Functions + +-- Math Rounding Credits: http://lua-users.org/wiki/SimpleRound +function QBCore.Functions.Math.Sign(v) + return (v >= 0 and 1) or -1 +end + +-- Math Rounding Credits: http://lua-users.org/wiki/SimpleRound +-- Usage: +-- QBCore.Functions.Math.Round(119.68, 0.01) = 119.68 +-- QBCore.Functions.Math.Round(119.68, 0.1) = 119.7 +-- QBCore.Functions.Math.Round(119.68) = 120 +-- QBCore.Functions.Math.Round(119.68, 100) = 100 +-- QBCore.Functions.Math.Round(119.68, 1000) = 0 +function QBCore.Functions.Math.Round(v, bracket) + bracket = bracket or 1 + return QBCore.Functions.Math.Sign(v/bracket + QBCore.Functions.Math.Sign(v) * 0.5) * bracket +end