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 32: Line 32:
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,-3)
int_part = int_part:sub(1,-4)
end
end

Revision as of 14:26, 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:Gaps'})
	local gap

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

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

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

	local int_part = math.floor(number)
	local frac_part = number - int_part
	
	int_part = tostring(int_part)
	
	local int_string = ''
	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
	
	if int_part:len() > 0 then
		int_string = int_string..'<span style="margin-left:'..gap..';">'..int_part..'</span>'
	end
	
	ret_string = ret_string..int_string
	
	if frac_part ~= 0 then
		frac_part = frac_part:tostring()
		local frac_string = '.'
		while frac_part:len() >= 3 do
			frac_string = frac_string..'<span style="margin-left:'..gap..';">'..frac_part:sub(1,4)..'</span>'
			frac_part = frac_part:sub(4)
		end
	
		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
	
	ret_string = ret_string..'</span>'

	return ret_string
end

return p