Jump to content
Toggle menu
  • 3.1K articles
  • 4.4K files
  • 18 users
  • 91.5K edits
Ashenzari's Archive
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Format

From Ashenzari's Archive
Revision as of 12:32, 11 October 2026 by Predelnik (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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