模組:沙盒/Ythlev
外观
local p = {}
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
for _, v in {'electorate', 'threshold', 'agree', 'disagree', 'invalid'} do
p[v] = tonumber(args[v]) or 0
end
local valid = args.agree + args.disagree
local lang = mw.getContentLanguage()
local function fmt(n)
return lang:formatNum(n)
end
local root = mw.html.create('table')
root
:addClass('wikitable')
:css('text-align', 'right')
:tag('caption')
:wikitext(args.caption)
:tag('tr')
:tag('th')
:tag('th')
:wikitext('票數')
:tag('th')
:wikitext('%')
:tag('th')
:wikitext('佔選舉人數%')
:tag('tr')
:tag('td')
:wikitext('選舉人數')
:tag('td')
:wikitext(fmt(electorate))
:tag('td')
:attr('colspan', 2)
:wikitext('100.00%')
:tag('tr')
:tag('td')
:wikitext('門檻票數')
:tag('td')
:wikitext(fmt(math.ceil(electorate * threshold)))
:tag('td')
:attr('colspan', 2)
:wikitext(string.format('%.2f%%', threshold * 100))
:tag('tr')
:tag('td')
:wikitext('同意票數')
:tag('td')
:wikitext(fmt(agree))
:tag('td')
:wikitext(string.format('%.2f%%', agree / valid * 100))
:tag('td')
:wikitext(string.format('%.2f%%', agree / electorate * 100))
:tag('tr')
:tag('td')
:wikitext('不同意票數')
:tag('td')
:wikitext(fmt(disagree))
:tag('td')
:wikitext(string.format('%.2f%%', disagree / valid * 100))
:tag('td')
:wikitext(string.format('%.2f%%', disagree / electorate * 100))
:tag('tr')
:tag('td')
:wikitext('有效票數')
:tag('td')
:wikitext(fmt(valid))
:tag('td')
:wikitext(string.format('%.2f%%', valid / (valid + invalid) * 100))
:tag('td')
:wikitext(string.format('%.2f%%', valid / electorate * 100))
:tag('tr')
:tag('td')
:wikitext('無效票數')
:tag('td')
:wikitext(fmt(invalid))
:tag('td')
:wikitext(string.format('%.2f%%', invalid / (valid + invalid) * 100))
:tag('td')
:wikitext(string.format('%.2f%%', invalid / electorate * 100))
:tag('tr')
:css('font-weight', 'bold')
:tag('td')
:wikitext('總投票數')
:tag('td')
:wikitext(fmt(valid + invalid))
:tag('td')
:wikitext('100.00%')
:tag('td')
:wikitext(string.format('%.2f%%', (valid + invalid) / electorate * 100))
:tag('tr')
:tag('td')
:wikitext('結果')
:tag('td')
:attr('colspan', 3)
:wikitext()
return tostring(root)
end
return p