From 2199ddef10c5dfdb3cb82ebff854f6c8a46f4bad Mon Sep 17 00:00:00 2001 From: Robert Zenz Date: Mon, 8 Dec 2014 12:21:43 +0100 Subject: [PATCH] Added round function. --- utils/mathutil.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/utils/mathutil.lua b/utils/mathutil.lua index b3eb075..101e46d 100644 --- a/utils/mathutil.lua +++ b/utils/mathutil.lua @@ -131,6 +131,19 @@ function mathutil.next_lower_prime(number) return 0 end +--- Rounds to the nearest number with the given decimal places. +-- +-- @param value The value to round. +-- @param decimal_places Optional. The number of decimal places to round to, +-- defaults to 0. +-- @return The rounded value. +function mathutil.round(value, decimal_places) + decimal_places = decimal_places or 0 + + local multiplicator = 10 ^ decimal_places + return math.floor(value * multiplicator + 0.5) / multiplicator +end + --- Performs a linear transform on the given value to transform the value -- from the range -1/1 to 0/1. --