Module:Addcommas

-- -- Taken from runescape.wikia.com/wiki/Module:Addcommas --

local p = {}

-- -- for adding commas to integers -- local function insert( str ) local rgx = '(%d+)(%d%d%d)'

while mw.ustring.find( str, rgx ) do       str = mw.ustring.gsub( str, rgx, '%1,%2' ) end

return str end

-- -- 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)' local y = mw.text.split( z, '-' ) local range

-- strip any existing commas z = p._strip( z )

-- look for a range which we need to handle differently -- but as a range should always be integers we can skip the fractional checks if y[1] ~= '' and y[2] then x = insert( y[1] ) .. '-' .. insert( y[2] ) else -- 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

x[1] = insert( x[1] )

-- 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 end

return x end

-- -- strips any existing commas in the input -- function p._strip( str ) return mw.ustring.gsub( tostring( str ), ',', '' ) end

function p.commas ( frame )

local str = frame:getParent.args[1] return p._commas( str )

end

return p