Module:Addcommas

-- -- Implements --

local p = {} local yesno = require( 'Module:Yesno' )

-- -- main function -- keep public so it can be used in other modules -- function p._commas( arg, strip )

local z = tostring( arg ) local x = {} local rgx = '(%d+)(%d%d%d)'

-- @todo strip any existing commas from the string -- strip commas here -- check if we want to add commas back in   if not yesno( strip ) then return end

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

function p.commas ( frame )

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

end

return p