Module:Math: Difference between revisions
no edit summary
m>Dragons flight (can't work due to prehandling by #expr, simplify) |
m>Dragons flight No edit summary |
||
Line 101: | Line 101: | ||
return min_value | return min_value | ||
end | |||
-- Rounds a number to specified precision | |||
function z.round(frame) | |||
local value = tonumber(frame.args[1] or frame.args.value or 0); | |||
local precision = tonumber(frame.args[2] or frame.args.precision or 0); | |||
return z._round( value, precision ); | |||
end | |||
function z._round( value, precision ) | |||
local rescale = math.pow( 10, precision ); | |||
return math.floor( value * rescale + 0.5 ) / rescale; | |||
end | end | ||
-- Rounds a number to the specified precision and formats according to rules | -- Rounds a number to the specified precision and formats according to rules | ||
-- originally used for {{template:Rnd}} | -- originally used for {{template:Rnd}}. Output is a string. | ||
function z. | function z.precision_format( frame ) | ||
-- For access to Mediawiki built-in formatter. | -- For access to Mediawiki built-in formatter. | ||
local lang = mw.getContentLanguage(); | local lang = mw.getContentLanguage(); | ||
Line 123: | Line 134: | ||
-- If rounding off, truncate extra digits | -- If rounding off, truncate extra digits | ||
if precision < current_precision then | if precision < current_precision then | ||
value = z._round( value, precision ); | |||
current_precision = z._precision( value ); | current_precision = z._precision( value ); | ||
end | end |