跳转到内容

模組:沙盒/Ythlev

维基百科,自由的百科全书

这是本页的一个历史版本,由Ythlev留言 | 贡献2020年6月20日 (六) 13:52编辑。这可能和当前版本存在着巨大的差异。

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