Module:Gapnum

local p = {}

local clean = require('Module:Math')._cleannumber local getArgs

function p.main(frame) if not getArgs then getArgs = require('Module:Arguments').getArgs end local args = getArgs(frame, {wrappers = 'Template:Gapnum'}) local gap

if args.gap then gap = args.gap else gap = '0.25em' end

local ret_string = ' '

local number = tonumber(frame:preprocess(args[1])) if not number then return 'Error: parameter 1 did not parse as a number.' end

local int_part = math.floor(number) int_part = tostring(int_part) -- Using substrings instead of math to avoid rounding errors local frac_part = tostring(number):sub(int_part:len+2) local int_string = '' while int_part:len >= 3 do		int_string = ' '..int_part:sub(-3)..' '..int_string int_part = int_part:sub(1,-4) end if int_part:len > 0 then int_string = ' '..int_part..' '..int_string end ret_string = ret_string..int_string local precision = tonumber(args.prec) or -1 if precision ~=0 and frac_part ~= 0 then frac_part = tostring(frac_part) local frac_string = '.' if precision > 0 then if precision < frac_part:len then frac_part = frac_part:sub(1,precision) elseif precision > frac_part:len then while frac_part:len < precision do					frac_part = frac_part..'0' end end end -- The first group after the decimal shouldn't have a gap if frac_part:len >= 3 then frac_string = frac_string..frac_part:sub(1,3) frac_part = frac_part:sub(4) else frac_string = frac_string..frac_part frac_part = '' end while frac_part:len >= 3 do			frac_string = frac_string..' '..frac_part:sub(1,3)..' ' frac_part = frac_part:sub(4) end if frac_part:len > 0 then frac_string = frac_string..' '..frac_part..' ' end ret_string = ret_string..frac_string end ret_string = ret_string..' '

return ret_string end

return p