Module:Currency

From Call of Duty Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local util_args = require('Module:ArgsUtil')
local util_math = require('Module:MathUtil')
local util_text = require('Module:TextUtil')
local lang = mw.getLanguage('en')

local h = {}
local p = require('Module:EntityAbstract'):finalExtends()
p.objectType = 'Currency'

function p:long(amount)
	if self.is_nil or not amount then return end
	if tonumber(amount) == 1 then
		return self.vars.singular:format(util_math.formatNumber(amount))
	end
	return self.vars.plural:format(util_math.formatNumber(amount))
end

function p:short(amount)
	if self.is_nil or not amount then return end
	local unpadded = self.vars.short:format(util_math.formatNumber(amount))
	return h.padDecimal(unpadded, self.vars.decimals)
end

function h.padDecimal(str, places)
	if not places then return str end
	local tbl = util_text.split(str,'.',true)
	if tbl[2] then
		local zeroes = {}
		local i = #tbl[2]
		while i < places do
			zeroes[#zeroes+1] = 0
			i = i + 1
		end
		tbl[2] = tbl[2] .. table.concat(zeroes,'')
		return table.concat(tbl,'.')
	end
	return str
end

return p