commit f1efd55: [Minor] Lua_util: Fix round function

Vsevolod Stakhov vsevolod at highsecure.ru
Sun Oct 11 21:35:07 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-10-11 22:32:01 +0100
URL: https://github.com/rspamd/rspamd/commit/f1efd55e9d9afdc0bdee58c7b54dbff361e85363 (HEAD -> master)

[Minor] Lua_util: Fix round function

---
 lualib/lua_util.lua | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index 521424966..bcb697e2c 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -124,10 +124,14 @@ end
 -- @return {number} rounded number
 --]]
 
--- Robert Jay Gould http://lua-users.org/wiki/SimpleRound
+-- modified version from Robert Jay Gould http://lua-users.org/wiki/SimpleRound
 exports.round = function(num, numDecimalPlaces)
   local mult = 10^(numDecimalPlaces or 0)
-  return math.floor(num * mult) / mult
+  if num >= 0 then
+    return math.floor(num * mult + 0.5) / mult
+  else
+    return math.ceil(num * mult - 0.5) / mult
+  end
 end
 
 --[[[


More information about the Commits mailing list