Module:Tabelrij met gemiddelde

Uit Wikipedia, de vrije encyclopedie

local p = {}

function p.main(frame)
	local args = frame:getParent().args
	local sum = 0
	local count = 0
	local r = ''
	do	-- iterate over numbered arguments
		local i = 1, v
		while true do
			v = args[i]
			if not v then
				break
			end
			r = r .. '||' .. v
			if v ~= "" then
				v = frame:callParserFunction( 'formatnum', {v, 'R'} )
				if v and v ~= "" then
					v = tonumber(v)
					if v then
						sum = sum + (v * 100)
						count = count + 1
					end
				end
			end
			i = i + 1
		end
	end
	local avg = sum / (count * 100)
	avg = math.floor(avg * 100 + 0.5) / 100 -- rounding in Scribunto is weird
	avg = string.format("%.2f", avg)
	avg = frame:callParserFunction( 'formatnum', {avg} )
	avg = '||' .. avg
	if args["plaats"] == "achter" then
		r = r .. avg
	else
		r = avg .. r
	end
	return r
end

return p