Code: Select all
function round(num, idp)
--Input: number to round; decimal places required
assert(num ~= nil, "Can't ROUND a nil value")
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
Thanks.
Code: Select all
function round(num, idp)
--Input: number to round; decimal places required
assert(num ~= nil, "Can't ROUND a nil value")
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
Code: Select all
function truncateToDecimal(num, dec) -- assume num is a number by default, assume dec is a number or nil by default
dec = dec or 0 -- default value
return num > 0 and math.floor(num * 10^dec)/10^dec or math.ceil(num * 10^dec)/10^dec -- works for both positives and negatives
end
Code: Select all
function math_round(val, decimal)
if not val then return 0 end
if (decimal) then
return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
else
return math.floor(val+0.5)
end
end
Code: Select all
function round(n, mult)
mult = mult or 1
return math.floor((n+mult/2)/mult) * mult
end
Code: Select all
local n = -.4
function round_1(num, idp)
--Input: number to round; decimal places required
assert(num ~= nil, "Can't ROUND a nil value")
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
function round_2(num, dec) -- assume num is a number by default, assume dec is a number or nil by default
dec = dec or 0 -- default value
return num > 0 and math.floor(num * 10^dec)/10^dec or math.ceil(num * 10^dec)/10^dec -- works for both positives and negatives
end
function round(n, mult)
mult = mult or 1
return math.floor((n+mult/2)/mult) * mult
end
local timeTest = function(f, ...)
local t1, r, t2 = os.clock(), f(...), os.clock()
print(string.format("The function took %0.6f seconds to run", t2 - t1))
return r
end
print("\nOriginal function:\n")
for i = 1, 10 do
timeTest(round_1, n)
end
print("\nZorg's function:\n")
for i = 1, 10 do
timeTest(round_2, n)
end
print("\nLast function:\n")
for i = 1, 10 do
timeTest(round, n)
end
Users browsing this forum: Bing [Bot] and 5 guests