Module:Utils: Difference between revisions

Fredg999 (talk | contribs)
m Added comment
Fredg999 (talk | contribs)
Updated round to use string library, also now uses 6 significant digits by default
Line 36: Line 36:
end
end


-- return x rounded to a precision of p decimal places
-- return x rounded to a precision of prec significant figures
function p.round(frame)
function p.round(frame)
local args = getArgs(frame)
local args = getArgs(frame)
Line 45: Line 45:
-- x defaults to 0
-- x defaults to 0
x = p.eval_num_arg(x, 0)
x = p.eval_num_arg(x, 0)
-- prec defaults to 3
-- prec defaults to 6
prec = p.eval_num_arg(prec, 3)
prec = p.eval_num_arg(prec, 6)
return math.floor(x*10^prec+0.5)/10^prec
return string.format(string.format("%%.%dg", prec), x)
end
end


return p
return p