Module:Round

From Coral Island Wiki
Jump to navigation Jump to search

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

local p = {}
local lib = require('Module:Feature')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrapper = { 'Template:Round' }
	})
	return p._main(args)
end

function p._main(args)
	local number	= args.num                 or args[1]           -- number
	local precision	= args.cut                 or args[2] or 0      -- precision
	local mode		= args.mode                or args[3] or 'auto' -- mode (auto/up/down)
	local ts		= tostring(args.ts) == '1' or false
	
	if type(number) == 'string' then
		number = number:gsub(',', '')
	end
	
	if mode == 'up' then
		number = math.ceil(number * (10^precision)) / (10^precision)
	elseif mode == 'down' then
		number = math.floor(number * (10^precision)) / (10^precision)
	else -- auto
		number = math.floor(number * (10^precision) + 0.5) / (10^precision)
	end
	
	if (tonumber(precision) > 0) then
		number = string.format("%.".. precision .."f", number)
	end
	
	if ts then
		return lib.thousandsSeparator(number)
	else
		return number
	end
end

return p