local p =
local getArgs
function p.main(frame) if not getArgs then getArgs = require('Module:Arguments').getArgs end
local args = getArgs(frame,) local n = args[1]
if not n then error('Parameter 1 is required') elseif not tonumber(n) and not tonumber(n, 36) then -- Validates any number with base ≤ 36 error('Unable to convert "' .. args[1] .. '" to a number') end
local gap = args.gap local precision = tonumber(args.prec)
return p.gaps(n,)end
-- Not named p._main so that it has a better function name when required by Module:Valfunction p.gaps(n,tbl) local nstr = tostring(n) if not tbl then tbl = end local gap = tbl.gap or '.25em'
local int_part, frac_part = p.groups(n,tbl.prec)
local ret = mw.html.create('span') :css('white-space','nowrap') -- No gap necessary on first group :wikitext(table.remove(int_part,1))
-- Build int part for _, v in ipairs(int_part) do ret:tag('span') :css('margin-left',gap) :wikitext(v) end
if frac_part then -- The first group after the decimal shouldn't have a gap ret:wikitext('.' .. table.remove(frac_part,1)) -- Build frac part for _, v in ipairs(frac_part) do ret:tag('span') :css('margin-left',gap) :wikitext(v) end end
return retend
-- Creates tables where each element is a different group of the numberfunction p.groups(num,precision) local nstr = tostring(num) if not precision then precision = -1 end
local decimalloc = nstr:find('.', 1, true) local int_part, frac_part if decimalloc
if precision ~= 0 and frac_part then ret_d = if precision
-- Allow groups of 3 or 2 (3 first) for v in string.gmatch(frac_part,'%d%d%d?') do table.insert(ret_d,v) end -- Preference for groups of 4 instead of groups of 1 at the end if #frac_part % 3
1 then ret_d = else local last_g = ret_d[#ret_d] or last_g = last_g..frac_part:sub(-1) ret_d[#ret_d] = last_g end end end
return ret_i,ret_dend
return p