localp={}localgetArgsfunctionp.main(frame)ifnotgetArgsthengetArgs=require('Module:Arguments').getArgsendlocalargs=getArgs(frame,{wrappers='Template:Gapnum'})localn=args[1]ifnotnthenerror('Parameter 1 is required')elseifnottonumber(n)andnottonumber(n,36)then-- Validates any number with base ≤ 36error('Unable to convert "'..args[1]..'" to a number')endlocalgap=args.gaplocalprecision=tonumber(args.prec)returnp.gaps(n,{gap=gap,prec=precision})end-- Not named p._main so that it has a better function name when required by Module:Valfunctionp.gaps(n,tbl)localnstr=tostring(n)ifnottblthentbl={}endlocalgap=tbl.gapor'.25em'localint_part,frac_part=p.groups(n,tbl.prec)localret=mw.html.create('span'):css('white-space','nowrap')-- No gap necessary on first group:wikitext(table.remove(int_part,1))-- Build int partfor_,vinipairs(int_part)doret:tag('span'):css('margin-left',gap):wikitext(v)endiffrac_partthen-- The first group after the decimal shouldn't have a gapret:wikitext('.'..table.remove(frac_part,1))-- Build frac partfor_,vinipairs(frac_part)doret:tag('span'):css('margin-left',gap):wikitext(v)endendreturnretend-- Creates tables where each element is a different group of the numberfunctionp.groups(num,precision)localnstr=tostring(num)ifnotprecisionthenprecision=-1endlocaldecimalloc=nstr:find('.',1,true)localint_part,frac_partifdecimalloc==nilthenint_part=nstrelseint_part=nstr:sub(1,decimalloc-1)frac_part=nstr:sub(decimalloc+1)end-- only define ret_i as an empty table, let ret_d stay nillocalret_i,ret_d={}-- Loop to handle most of the groupings; from right to left, so that if a group has less than 3 members, it will be the first groupwhileint_part:len()>3do-- Insert in first spot, since we're moving backwardstable.insert(ret_i,1,int_part:sub(-3))int_part=int_part:sub(1,-4)end-- handle any left over numbersifint_part:len()>0thentable.insert(ret_i,1,int_part)endifprecision~=0andfrac_partthenret_d={}ifprecision==-1thenprecision=frac_part:len()end-- Reduce the length of the string if required precision is less than actual precision-- OR-- Increase it (by adding 0s) if the required precision is more than actuallocaloffset=precision-frac_part:len()ifoffset<0thenfrac_part=frac_part:sub(1,precision)elseifoffset>0thenfrac_part=frac_part..string.rep('0',offset)end-- Allow groups of 3 or 2 (3 first)forvinstring.gmatch(frac_part,'%d%d%d?')dotable.insert(ret_d,v)end-- Preference for groups of 4 instead of groups of 1 at the endif#frac_part%3==1theniffrac_part:len()==1thenret_d={frac_part}elselocallast_g=ret_d[#ret_d]or''last_g=last_g..frac_part:sub(-1)ret_d[#ret_d]=last_gendendendreturnret_i,ret_dendreturnp