From 2e926f0c11fc90696333e3a33ed9b246184faeef Mon Sep 17 00:00:00 2001 From: hasezoey Date: Sat, 30 Mar 2024 11:11:43 +0100 Subject: [PATCH] Util: add function "round_decimal" to round to a decimal point 0.00001, decimal 2 -> 0.00 --- frontend/util.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/frontend/util.lua b/frontend/util.lua index 817b0b740..62c4cfd83 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -1537,4 +1537,12 @@ function util.wrapMethod(target_table, target_field_name, new_func, before_callb return wrapped end +-- Round a given "num" to the decimal points of "points" +-- (i.e. `round_decimal(0.000000001, 2)` will yield `0.00`) +function util.round_decimal(num, points) + local op = 10 ^ points + + return math.floor(num * op) / op +end + return util