Module:Format
From Ashenzari's Archive
More actions
Module for common formatting functions
local p = {}
-- Each second, fourth and so on value are considered a weight and transformed into a percent
function p.weighted_list(frame)
local pframe = frame:getParent()
local pargs = nil
if pframe ~= nil then
pargs = pframe.args
else
return ''
end
if pargs == nil then
return ''
end
local result = ''
local sum = 0
for i,arg in ipairs(pargs) do
if i % 2 == 0 then
sum = sum + tonumber(arg)
end
end
for i,arg in ipairs(pargs) do
if i % 2 == 0 then
result = result .. string.format("%.3g%%", tonumber(arg) / sum * 100)
else
result = result .. arg
end
end
return result
end
return p