Module:Gapnum: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>The Mol Man
No edit summary
meta>The Mol Man
No edit summary
Line 8: Line 8:
end
end
local args = getArgs(frame, {wrappers = 'Template:Gapnum'})
local args = getArgs(frame, {wrappers = 'Template:Gapnum'})
local gap
local gap = args.gap or '0.25em'
 
if args.gap then
gap = args.gap
else
gap = '0.25em'
end


local ret_string = '<span style="white-space:nowrap">'
local ret_string = '<span style="white-space:nowrap">'


-- preprocess to handle [[Template:Convert/gaps]], which uses {{formatnum:}}
local number = tonumber(frame:preprocess(args[1]))
local number = tonumber(frame:preprocess(args[1]))
Line 24: Line 19:
end
end


-- Split number into 2 parts: integer part and decimal part
local int_part = math.floor(number)
local int_part = math.floor(number)
int_part = tostring(int_part)
int_part = tostring(int_part)
Line 29: Line 25:
local frac_part = tostring(number):sub(int_part:len()+2)
local frac_part = tostring(number):sub(int_part:len()+2)
-- If the frac_part is empty, reinterpret it as 0
if frac_part:len() == 0 then
if frac_part:len() == 0 then
frac_part = tonumber(0)
frac_part = tonumber(0)
Line 34: Line 31:
local int_string = ''
local int_string = ''
-- 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 group
while int_part:len() >= 3 do
while int_part:len() >= 3 do
int_string = '<span style="margin-left:'..gap..';">'..int_part:sub(-3)..'</span>'..int_string
int_string = '<span style="margin-left:'..gap..';">'..int_part:sub(-3)..'</span>'..int_string
int_part = int_part:sub(1,-4)
int_part = int_part:sub(1,-4)
end
end
-- handle any left over numbers
if int_part:len() > 0 then
if int_part:len() > 0 then
int_string = '<span style="margin-left:'..gap..';">'..int_part..'</span>'..int_string
int_string = '<span style="margin-left:'..gap..';">'..int_part..'</span>'..int_string
Line 46: Line 45:
local precision = tonumber(args.prec) or -1
local precision = tonumber(args.prec) or -1
-- if the precision is NOT 0 (a whole number) and the decimal part is NOT empty (i.e. 0)
if precision ~=0 and frac_part ~= 0 then
if precision ~=0 and frac_part ~= 0 then
frac_part = tostring(frac_part)
frac_part = tostring(frac_part)
local frac_string = '.'
local frac_string = '.'
-- 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 actual
if precision > 0 then
if precision > 0 then
if precision < frac_part:len() then
if precision < frac_part:len() then
Line 60: Line 63:
end
end
-- The first group after the decimal shouldn't have a gap
-- The first group after the decimal shouldn't have a gap between the decimal
if frac_part:len() >= 3 then
if frac_part:len() >= 3 then
frac_string = frac_string..frac_part:sub(1,3)
frac_string = frac_string..frac_part:sub(1,3)
Line 69: Line 72:
end
end
-- Loop to handle most of the groupings; from left to right, so that if a group has
-- less than 3 members, it will be the last group
while frac_part:len() >= 3 do
while frac_part:len() >= 3 do
frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part:sub(1,3)..'</span>'
frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part:sub(1,3)..'</span>'
Line 74: Line 79:
end
end
-- Handle any left over numbers
if frac_part:len() > 0 then
if frac_part:len() > 0 then
frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part..'</span>'
frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part..'</span>'
Line 80: Line 86:
end
end
-- Closing span tag
ret_string = ret_string..'</span>'
ret_string = ret_string..'</span>'



Revision as of 15:23, 5 July 2014

Documentation for this module may be created at Module:Gapnum/doc

local p = {}

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 =  args.gap or '0.25em'

	local ret_string = '<span style="white-space:nowrap">'

	-- preprocess to handle [[Template:Convert/gaps]], which uses {{formatnum:}}
	local number = tonumber(frame:preprocess(args[1]))
	
	if not number then
		return 'Error: parameter 1 did not parse as a number.'
	end

	-- Split number into 2 parts: integer part and decimal part
	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)
	
	-- If the frac_part is empty, reinterpret it as 0
	if frac_part:len() == 0 then
		frac_part = tonumber(0)
	end
	
	local int_string = ''
	-- 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 group
	while int_part:len() >= 3 do
		int_string = '<span style="margin-left:'..gap..';">'..int_part:sub(-3)..'</span>'..int_string
		int_part = int_part:sub(1,-4)
	end
	-- handle any left over numbers
	if int_part:len() > 0 then
		int_string = '<span style="margin-left:'..gap..';">'..int_part..'</span>'..int_string
	end
	
	ret_string = ret_string..int_string
	
	local precision = tonumber(args.prec) or -1
	-- if the precision is NOT 0 (a whole number) and the decimal part is NOT empty (i.e. 0)
	if precision ~=0 and frac_part ~= 0 then
		frac_part = tostring(frac_part)
		local frac_string = '.'
		
		-- 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 actual
		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 between the decimal
		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
		
	-- Loop to handle most of the groupings; from left to right, so that if a group has
	-- less than 3 members, it will be the last group
		while frac_part:len() >= 3 do
			frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part:sub(1,3)..'</span>'
			frac_part = frac_part:sub(4)
		end
	
		-- Handle any left over numbers
		if frac_part:len() > 0 then
			frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part..'</span>'
		end
		ret_string = ret_string..frac_string
	end
	
	-- Closing span tag
	ret_string = ret_string..'</span>'

	return ret_string
end

return p