FANDOM


--[[
{{Helper module
|name = Currency short
|fname1 = _amount(a, coinType, precise)
|ftype1 = number/string, string, boolean
|fuse1 = Returns the number a with the correct image in front of it and formatted with commas. Also shortens the number to k or M if needed unless <code>precise</code> = <code>true</code>. Current supported cointypes are <code>coins</code>, <code>rusty</code> and <code>chimes</code>.
}}
-- ]]
-- <pre>
--
-- Implements various currency templates
--
 
local p = {}
local YesNo = require("Module:Yesno")
 
local function amount (a, coinType, precise)
    -- 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 = {'<span class="coins ', true, true, true, true, "", '</span>'}
    -- add class for CSS to add the correct image with
    -- see [[MediaWiki:Custom-Common.less/coins.less]] for more details
    local coinClasses = {
        coins = 'coins-',
        rusty = 'rusty-coins-',
        chimes = 'chimes-'
    }
    local a2, num, amounts, i, j
 
    ret[1] = '<span class="coins '
    ret[2] = coinClasses[coinType]
 
    -- strip commas from input
    -- @example {{GEPrice|Foo}} -> '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}
    amountsChimes = {1000, 100, 50, 20, 1}
 
    if coinType == 'chimes' then
        amts = amountsChimes
    else
        amts = amounts
    end
 
    for i = 1, #amts do
        j = amts[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
 
    -- shorten the number k or M
	if not precise then
        if math.abs(a) >= 10000000 then
    	    a = math.floor(a2 / 100000 + 0.5) / 10
    	    ret[6] = "<b>M</b>"
    	elseif math.abs(a) >= 100000 then
    	    a = math.floor(a2 / 1000 + 0.5)
    	    ret[6] = "<b>k</b>"
        else
            a = math.floor(a2 + 0.5)
        end
	end
 
    -- format number with commas
    ret[5] = mw.language.getContentLanguage():formatNum(a)
 
    return table.concat( ret )
end
 
--
-- {{Coins short}}
--
function p.coins(frame)
    local args = frame:getParent().args
    local a = args[1] or '0'
    local pr = YesNo(args[2]) or false
    return amount(a, 'coins', pr)
end
 
--
-- {{Rusty coins short}}
--
function p.rusty(frame)
    local args = frame:getParent().args
    local a = args[1] or '0'
    local pr = YesNo(args[2]) or false
    return amount(a, 'rusty', pr)
end
 
--
-- {{Chimes short}}
--
function p.chimes(frame)
    local args = frame:getParent().args
    local a = args[1] or '0'
    local pr = YesNo(args[2]) or false
    return amount(a, 'chimes', pr)
end
 
--
-- Module access point
--
function p._amount(a, coinType, precise)
    a = tostring(a) or '0'
    return amount(a, coinType, precise)
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.