Jump to content

Module:Spel number

Di Wikipedia, e ensiklopedia liber

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

local p = {}
local words_default = {"mil", "miyon", "biyon", "triyon"}  -- Default variant
local words_c = {"mil", "mion", "bion", "trion"}           -- Second variant
local zero_default = "cero"
local zero_c = "sero"

-- For use by other scripts. Takes arguments:
-- - 1: string or number, value to convert
-- - forcenum: string for Template:Yesno, forces a result in digits for all n ≥ 10.
-- - variante: specifies the variant ('c' for the second variant, default for the default variant)
-- - formating options for number pa palabra: zero, adj, ord, us
function p.spellnum(args)
	local frame = mw.getCurrentFrame()
	local numeral = tonumber(args[1])
    local variante = args['variante'] or ''
    local words = variante == 'c' and words_c or words_default
    local pass_zero = variante == 'c' and zero_c or zero_default
    
    if args['zero'] ~= nil and args['zero'] ~= '' then
    	pass_zero = args['zero']
    end
    
	-- Always return numerals for negative numbers, non-integers, and if (forcenum and numeral >= 10).
	if numeral < 0 or
			math.fmod(numeral, 1) ~= 0 or
			(numeral >= 10 and frame:expandTemplate{ title = 'yesno', args = {args['forcenum']} } == 'yes') then
		return mw.language.getContentLanguage():formatNum(numeral)
	end

	-- Convert numeral to words
	local spelled = frame:expandTemplate{ title = 'number pa palabra', args = {
		numeral, zero = pass_zero, adj = args['adj'], ord = args['ord'], us = args['us']}}
	
	-- Return numerals if more than two words would be needed, else return words
	if mw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+') then
		-- Handle numbers larger than one million
		if numeral >= 1000000 and numeral <= 1000000000000000 then
			local size = math.min(4, math.floor(math.log10(numeral) / 3))
			numeral = numeral / 1000^size
			return ({"%.1f ", "%d ", "%d "})[1 + math.floor(math.log10(numeral))]:format(numeral) .. words[size]
		end
		return mw.language.getContentLanguage():formatNum(numeral)
	else 
		return spelled
	end
end

function p.main(frame)
	return p.spellnum(frame.args)
end

return p