Module:Utils: Difference between revisions

Plumtree (talk | contribs)
round(): %g -> %f - the former uses exponential notation when it's shorter than floating point representation; it's undesirable
Plumtree (talk | contribs)
m round() improved
Line 47: Line 47:
-- prec defaults to 6
-- prec defaults to 6
prec = p.eval_num_arg(prec, 6)
prec = p.eval_num_arg(prec, 6)
return string.format(string.format("%%.%df", prec), x)
local s = string.format(string.format("%%.%df", prec), x)
-- remove unnecessary zeros
if s:find('%.') then
while s:sub(-1) == '0' do
s = s:sub(1, -2)
end
-- remove unnecessary .
if s:sub(-1) == '.' then
s = s:sub(1, -2)
end
end
return s
end
end