Module:Coins2: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
(Created page with "-- <pre> -- Implements {{Coins}} -- Original source: http://runescape.wiki/w/Module:Coins?action=history local p = {} local function amount(a) -- 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 coins-', true, true, true, '</span>'} -- strip commas from input -- @example '123,456,789'...")
 
No edit summary
Line 1: Line 1:
-- <pre>
-- Implements {{Coins}}
-- Original source: http://runescape.wiki/w/Module:Coins?action=history
local p = {}
local p = {}


local function amount(a)
local function amount(a)
-- convert used globals to locals where possible to improve performance
    -- convert used globals to locals where possible to improve performance
local math = math
    local math = math
local string = string
    local string = string
local table = table
    local table = table
local mw = mw
    local mw = mw
        local expr = mw.ext.ParserFunctions.expr
    local expr = mw.ext.ParserFunctions.expr
 
local ret = {'<span class="coins coins-', true, true, true, '</span>'}
 
-- strip commas from input
-- @example '123,456,789'
a = string.gsub(a, ',', '')
-- check for decimals/division to determine if output should have forced 2 decimal places
local has_decimal_or_division = false
if string.match(a, "[./]") then
has_decimal_or_division = true
end
-- for performance reasons, only calculate expr if required
if tonumber(a) == nil then
a = tonumber(expr(a)) or 0
end
-- round to two decimal places
a = math.floor(a * 100 + 0.5) / 100
 
local num = math.abs(a)
local amounts = {1000, 250, 100, 25, 5, 4, 3, 2, 1}
local result = 1000
for _, amount in ipairs(amounts) do
result = amount
if num >= amount then
break
end
end
 
ret[2] = tostring(result)


-- set a class to denote positive or negative (css sets the colour)
    -- strip commas from input
if a > 0 then
    a = string.gsub(a, ',', '')
ret[3] = ' coins-pos">'
   
elseif a < 0 then
    -- check for decimals/division to determine if output should have forced 2 decimal places
ret[3] = ' coins-neg">'
    local has_decimal_or_division = false
else
    if string.match(a, "[./]") then
ret[3] = '">'
        has_decimal_or_division = true
end
    end
   
-- format number with commas
    -- for performance reasons, only calculate expr if required
if has_decimal_or_division then
    if tonumber(a) == nil then
local b = 0
        a = tonumber(expr(a)) or 0
a, b = math.modf(a)
    end
b = math.abs(b)
   
-- hack fix for formatNum stripping the sign from -0
    -- round to two decimal places
if (a > 999 or a < -999) then
    a = math.floor(a * 100 + 0.5) / 100
a = mw.language.getContentLanguage():formatNum(a)
end
b = string.sub(string.format("%.2f", b),2)
ret[4] = a..b
else
ret[4] = mw.language.getContentLanguage():formatNum(a)
end


return table.concat( ret )
    -- format number with commas
    if has_decimal_or_division then
        local b = 0
        a, b = math.modf(a)
        b = math.abs(b)
        -- hack fix for formatNum stripping the sign from -0
        if (a > 999 or a < -999) then
            a = mw.language.getContentLanguage():formatNum(a)
        end
        b = string.sub(string.format("%.2f", b),2)
        return a .. b
    else
        return mw.language.getContentLanguage():formatNum(a)
    end
end
end


Line 77: Line 46:
--
--
function p.amount(frame)
function p.amount(frame)
local args = frame:getParent().args
    local args = frame:getParent().args
-- for {{coins|111}}
    -- for {{coins|111}}
local a = args[1] or '0'
    local a = args[1] or '0'
return amount(a)
    return amount(a)
end
end


Line 87: Line 56:
--
--
function p._amount(a)
function p._amount(a)
a = tostring(a) or '0'
    a = tostring(a) or '0'
return amount(a)
    return amount(a)
end
end


return p
return p

Revision as of 09:26, 19 May 2024

Documentation for this module may be created at Module:Coins2/doc

local p = {}

local function amount(a)
    -- 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

    -- strip commas from input
    a = string.gsub(a, ',', '')
    
    -- check for decimals/division to determine if output should have forced 2 decimal places
    local has_decimal_or_division = false
    if string.match(a, "[./]") then
        has_decimal_or_division = true
    end
    
    -- for performance reasons, only calculate expr if required
    if tonumber(a) == nil then
        a = tonumber(expr(a)) or 0
    end
    
    -- round to two decimal places
    a = math.floor(a * 100 + 0.5) / 100

    -- format number with commas
    if has_decimal_or_division then
        local b = 0
        a, b = math.modf(a)
        b = math.abs(b)
        -- hack fix for formatNum stripping the sign from -0
        if (a > 999 or a < -999) then
            a = mw.language.getContentLanguage():formatNum(a)
        end
        b = string.sub(string.format("%.2f", b),2)
        return a .. b
    else
        return mw.language.getContentLanguage():formatNum(a)
    end
end

--
-- {{Coins}} access point
--
function p.amount(frame)
    local args = frame:getParent().args
    -- for {{coins|111}}
    local a = args[1] or '0'
    return amount(a)
end

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

return p