FANDOM


-- 
-- Implements {{Addcommas}}
--
 
local p = {}
 
--
-- main function
-- keep public so it can be used in other modules
--
function p._add( arg )
 
    local z = tostring( arg )
    local x = {}
    local rgx = '(%d+)(%d%d%d)'
 
    -- strip any existing commas
    z = p._strip( z )
 
    -- split number into integer and fractional parts
    -- this strips any trailing zeroes on the fractional part
    -- and any leading zeroes in the integer part
    -- no idea why, maybe something in the implementation of patterns?
    for i in mw.ustring.gmatch( z, '-*%d+' ) do
        x[#x + 1] = i
    end
 
    -- add commas
    while mw.ustring.find( x[1], rgx ) do
        x[1] = mw.ustring.gsub( x[1], rgx, '%1,%2' )
    end
 
    -- if the fractional part exists, then prepend it with a period
    -- and append it to the integer part
    if x[2] then
        x = x[1] .. '.' .. x[2]
    else
        x = x[1]
    end
 
    return x
end
 
--
-- strips any existing commas in the input
--
function p._strip( str )
    return mw.ustring.gsub( str, ',', '' )
end
 
function p.commas ( frame )
 
    local str = frame:getParent().args[1]
    return p._commas( str )
 
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.