Module:Currency

-- -- Implements various currency templates --

local p = {}

local function amount (a, coinType) -- convert used globals to locals where possible to improve performance local math = math local string = string local table = table local mw = mw       local expr = mw.ext.ParserFunctions.expr

local ret = {' '1,000' a = string.gsub(a, ',', '')

-- cache tonumber result a2 = tonumber(a)

-- only do this if required so as not to impact performance too much if a2 == nil then a = expr(a) a2 = tonumber(a) or 0 end

-- round to 2 d.p.	a = math.floor(a2 * 100 + 0.5) / 100

-- select which image class to use for css to hook off num = math.abs(a) amounts = {10000, 1000, 250, 100, 25, 5, 4, 3, 2, 1}

for i = 1, #amounts do		j = amounts[i]

if num >= j then break end end

ret[3] = tostring(j)

-- set a class to denote positive or negative (css sets the colour) if a > 0 then ret[4] = ' coins-pos">'	elseif a < 0 then		ret[4] = ' coins-neg">' else ret[4] = '">'	end

-- format number with commas ret[5] = mw.language.getContentLanguage:formatNum(a)

return table.concat( ret ) end

-- -- -- function p.coins(frame) local args = frame:getParent.args -- for or 	-- @todo remove named arg local a = args[1] or args.Amount or args.amount or '0' return amount(a, 'coins') end

-- -- -- function p.rusty(frame) local args = frame:getParent.args -- @todo remove named arg local a = args[1] or args.Amount or args.amount or '0' return amount(a, 'rusty') end

-- -- -- function p.chimes(frame) local args = frame:getParent.args -- @todo remove named arg local a = args[1] or args.Amount or args.amount or '0' return amount(a, 'chimes') end

-- -- Module access point -- function p._amount(a, coinType) a = tostring(a) or '0' return amount(a, coinType) end

return p