-- Módulo para conversão entre diferentes representações de números. Consulte a página de discussão para obter a documentação do usuário.
-- Para testes de unidade, consulte [[Módulo:ConvertNumeric/Testes]]
-- Ao editar, visualize com: [[Módule Discussão:ConvertNumeric/Testes]]
-- Primeiro, edite [[Módulo:ConvertNumeric/sandbox]], em seguida, visualize com [[Módulo Discussão:ConvertNumeric/sandbox/Testes]]
require('strict')

local ones_position = {
	[0] = 'zero',
	[1] = 'um',
	[2] = 'dois',
	[3] = 'três',
	[4] = 'quatro',
	[5] = 'cinco',
	[6] = 'seis',
	[7] = 'sete',
	[8] = 'oito',
	[9] = 'nove',
	[10] = 'dez',
	[11] = 'onze',
	[12] = 'doze',
	[13] = 'treze',
	[14] = 'quatorze',
	[15] = 'quinze',
	[16] = 'dezesseis',
	[17] = 'dezessete',
	[18] = 'dezoito',
	[19] = 'dezenove'
}

local ones_position_ord = {
	[0] = 'zerésimo',
	[1] = 'primeiro',
	[2] = 'segundo',
	[3] = 'terceiro',
	[4] = 'quarto',
	[5] = 'quinto',
	[6] = 'sexto',
	[7] = 'sétimo',
	[8] = 'oitavo',
	[9] = 'nono',
	[10] = 'décimo',
	[11] = 'décimo primeiro',
	[12] = 'décimo segundo',
	[13] = 'décimo terceiro',
	[14] = 'décimo quarto',
	[15] = 'décimo quinto',
	[16] = 'décimo sexto',
	[17] = 'décimo sétimo',
	[18] = 'décimo oitavo',
	[19] = 'décimo nono'
}

local ones_position_plural = {
	[0] = 'zeros',
	[1] = 'uns',
	[2] = 'dois',
	[3] = 'treses',
	[4] = 'quatros',
	[5] = 'cincos',
	[6] = 'seis',
	[7] = 'setes',
	[8] = 'oitos',
	[9] = 'noves',
	[10] = 'dezes',
	[11] = 'onzes',
	[12] = 'dozes',
	[13] = 'trezes',
	[14] = 'quatorzes',
	[15] = 'quinzes',
	[16] = 'dezesseis',
	[17] = 'dezessetes',
	[18] = 'dezoitos',
	[19] = 'dezenoves'
}

local tens_position = {
	[2] = 'vinte',
	[3] = 'trinta',
	[4] = 'quarenta',
	[5] = 'cinquenta',
	[6] = 'sessenta',
	[7] = 'setenta',
	[8] = 'oitenta',
	[9] = 'noventa'
}

local tens_position_ord = {
	[2] = 'vigésimo',
	[3] = 'trigésimo',
	[4] = 'quadragésimo',
	[5] = 'quinquagésimo',
	[6] = 'sexagésimo',
	[7] = 'septuagésimo',
	[8] = 'octogésimo',
	[9] = 'nonagésimo'
}

local tens_position_plural = {
	[2] = 'vintes',
	[3] = 'trintas',
	[4] = 'quarentas',
	[5] = 'cinquentas',
	[6] = 'sessentas',
	[7] = 'setentas',
	[8] = 'oitentas',
	[9] = 'noventas'
}

local hundreds_position = {
	[1] = 'cem',
	[2] = 'duzentos',
	[3] = 'trezentos',
	[4] = 'quatrocentos',
	[5] = 'quinhentos',
	[6] = 'seiscentos',
	[7] = 'setecentos',
	[8] = 'oitocentos',
	[9] = 'novecentos'
}

local hundreds_position_ord = {
	[1] = 'centésimo',
	[2] = 'ducentésimo',
	[3] = 'trecentésimo',
	[4] = 'quadringentésimo',
	[5] = 'quingentésimo',
	[6] = 'seiscentésimo',
	[7] = 'septingentésimo',
	[8] = 'octingentésimo',
	[9] = 'nongentésimo'
}

local hundreds_position_plural = {
	[1] = 'cens',
	[2] = 'duzentos',
	[3] = 'trezentos',
	[4] = 'quatrocentos',
	[5] = 'quinhentos',
	[6] = 'seiscentos',
	[7] = 'setecentos',
	[8] = 'oitocentos',
	[9] = 'novecentos'
}

local groups = {
	[1] = 'mil',
	[2] = 'milhão',
	[3] = 'bilhão',
	[4] = 'trilhão',
	[5] = 'quadrilhão',
	[6] = 'quintillion',
	[7] = 'sextillion',
	[8] = 'septillion',
	[9] = 'octillion',
	[10] = 'nonillion',
	[11] = 'decillion',
	[12] = 'undecillion',
	[13] = 'duodecillion',
	[14] = 'tredecillion',
	[15] = 'quattuordecillion',
	[16] = 'quindecillion',
	[17] = 'sexdecillion',
	[18] = 'septendecillion',
	[19] = 'octodecillion',
	[20] = 'novemdecillion',
	[21] = 'vigintillion',
	[22] = 'unvigintillion',
	[23] = 'duovigintillion',
	[24] = 'tresvigintillion',
	[25] = 'quattuorvigintillion',
	[26] = 'quinquavigintillion',
	[27] = 'sesvigintillion',
	[28] = 'septemvigintillion',
	[29] = 'octovigintillion',
	[30] = 'novemvigintillion',
	[31] = 'trigintillion',
	[32] = 'untrigintillion',
	[33] = 'duotrigintillion',
	[34] = 'trestrigintillion',
	[35] = 'quattuortrigintillion',
	[36] = 'quinquatrigintillion',
	[37] = 'sestrigintillion',
	[38] = 'septentrigintillion',
	[39] = 'octotrigintillion',
	[40] = 'noventrigintillion',
	[41] = 'quadragintillion',
	[51] = 'quinquagintillion',
	[61] = 'sexagintillion',
	[71] = 'septuagintillion',
	[81] = 'octogintillion',
	[91] = 'nonagintillion',
	[101] = 'centillion',
	[102] = 'uncentillion',
	[103] = 'duocentillion',
	[104] = 'trescentillion',
	[111] = 'decicentillion',
	[112] = 'undecicentillion',
	[121] = 'viginticentillion',
	[122] = 'unviginticentillion',
	[131] = 'trigintacentillion',
	[141] = 'quadragintacentillion',
	[151] = 'quinquagintacentillion',
	[161] = 'sexagintacentillion',
	[171] = 'septuagintacentillion',
	[181] = 'octogintacentillion',
	[191] = 'nonagintacentillion',
	[201] = 'ducentillion',
	[301] = 'trecentillion',
	[401] = 'quadringentillion',
	[501] = 'quingentillion',
	[601] = 'sescentillion',
	[701] = 'septingentillion',
	[801] = 'octingentillion',
	[901] = 'nongentillion',
	[1001] = 'millinillion',
}

local roman_numerals = {
	I = 1,
	V = 5,
	X = 10,
	L = 50,
	C = 100,
	D = 500,
	M = 1000
}

local ptord_tens_end  = {
	['vigésimo']	= 20,
	['trigésimo']	= 30,
	['quadragésimo']	= 40,
	['quinquagésimo']	= 50,
	['sexagésimo']	= 60,
	['septuagésimo']	= 70,
	['octogésimo']	= 80,
	['nonagésimo']	= 90,
}

local pt_tens_cont = {
	['vinte']	= 20,
	['trinta']	= 30,
	['quarenta']	= 40,
	['cinquenta']	= 50,
	['sessenta']	= 60,
	['setenta']	= 70,
	['oitenta']	= 80,
	['noventa']	= 90,
}

-- Converte um determinado numeral romano válido (e alguns numerais romanos inválidos) em um número. Retorna { -1, errorstring } em caso de erro.
local function roman_to_numeral(roman)
	if type(roman) ~= "string" then return -1, "roman numeral not a string" end
	local rev = roman:reverse()
	local raising = true
	local last = 0
	local result = 0
	for i = 1, #rev do
		local c = rev:sub(i, i)
		local next = roman_numerals[c]
		if next == nil then return -1, "numeral romano contém caracteres ilegais " .. c end
		if next > last then
			result = result + next
			raising = true
		elseif next < last then
			result = result - next
			raising = false
		elseif raising then
			result = result + next
		else
			result = result - next
		end
		last = next
	end
	return result
end

-- Converte um determinado número inteiro, entre 0 e 100, para texto em português (por exemplo, 47 -> quarenta e sete).
local function numeral_to_portuguese_less_100(num, ordinal, plural, zero)
	local terminal_ones, terminal_tens
	if ordinal then
		terminal_ones = ones_position_ord
		terminal_tens = tens_position_ord
	elseif plural then
		terminal_ones = ones_position_plural
		terminal_tens = tens_position_plural
	else
		terminal_ones = ones_position
		terminal_tens = tens_position
	end

	if num == 0 and zero ~= nil then
		return zero
	elseif num < 20 then
		return terminal_ones[num]
	elseif num % 10 == 0 then
		return terminal_tens[num / 10]
	else
		return tens_position[math.floor(num / 10)] .. ' e ' .. terminal_ones[num % 10]
	end
end

local function standard_suffix(ordinal, plural)
	if ordinal then return 'º' end
	if plural then return 's' end
	return ''
end

-- Converte um determinado número inteiro (na forma de string) entre 0 e 1000 para texto em português (por exemplo, 47 -> quarenta e sete).
local function numeral_to_portuguese_less_1000(num, use_and, ordinal, plural, zero)
	num = tonumber(num)
	if num < 100 then
		return numeral_to_portuguese_less_100(num, ordinal, plural, zero)
	elseif num % 100 == 0 then
		return hundreds_position[num/100] .. standard_suffix(ordinal, plural)
	else
		return hundreds_position[math.floor(num/100)] .. ' e ' .. numeral_to_portuguese_less_100(num % 100, ordinal, plural, zero)
	end
end

-- Converte um número ordinal, em texto em português, de 'zerésimo' a 'nonagésimo nono' para um número [0–99], senão -1.
local function portuguese_to_ordinal(portuguese)
	local pt = string.lower(portuguese or '')

	local ptord_lt20 = {} -- ones_position_ord{} keys & values swapped
	for k, v in pairs( ones_position_ord ) do
		ptord_lt20[v] = k
	end

	if ptord_lt20[pt] then
		return ptord_lt20[pt] -- por exemplo, primeiro -> 1
	elseif ptord_tens_end[pt] then
		return ptord_tens_end[pt] -- por exemplo, nonagésimo -> 90
	else
		local tens, ones = string.match(pt, '^([a-z]+)[%s%-]+([a-z]+)$')
		if tens and ones then
			local tens_cont = pt_tens_cont[tens]
			local ones_end  = ptord_lt20[ones]
			if tens_cont and ones_end then
				return tens_cont + ones_end -- por exemplo, nonagésimo nono -> 99
			end
		end
	end
	return -1 -- Falhou
end

-- Converte um número, em texto em português, de 'zero' a 'noventa e nove' para um número [0–99], senão -1.
local function portuguese_to_numeral(portuguese)
	local pt = string.lower(portuguese or '')

	local pt_lt20 = { ['single'] = 1 } -- ones_position{} keys & values swapped
	for k, v in pairs( ones_position ) do
		pt_lt20[v] = k
	end

	if pt_lt20[pt] then
		return pt_lt20[pt] -- por exemplo, um -> 1
	elseif pt_tens_cont[pt] then
		return pt_tens_cont[pt] -- por exemplo,  noventa -> 90
	else
		local tens, ones = string.match(pt, '^([a-z]+)[%s%-]+([a-z]+)$')
		if tens and ones then
			local tens_cont = pt_tens_cont[tens]
			local ones_end  = pt_lt20[ones]
			if tens_cont and ones_end then
				return tens_cont + ones_end -- por exemplo,  noventa e nove -> 99
			end
		end
	end
	return -1 -- Falhou
end

-- Converte um número expresso como uma string em notação científica em uma string em notação decimal padrão
-- por exemplo,  1.23E5 -> 123000, 1.23E-5 = .0000123. A conversão é exata, nenhum arredondamento é executado.
local function scientific_notation_to_decimal(num)
	local exponent, subs = num:gsub("^%-?%d*%.?%d*%-?[Ee]([+%-]?%d+)$", "%1")
	if subs == 0 then return num end  -- A entrada não está em notação científica, apenas retorna sem modificações
	exponent = tonumber(exponent)

	local negative = num:find("^%-")
	local _, decimal_pos = num:find("%.")
	-- Mantissa consistirá em todos os dígitos decimais sem ponto decimal
	local mantissa = num:gsub("^%-?(%d*)%.?(%d*)%-?[Ee][+%-]?%d+$", "%1%2")
	if negative and decimal_pos then decimal_pos = decimal_pos - 1 end
	if not decimal_pos then decimal_pos = #mantissa + 1 end

	-- Remove os zeros à esquerda, a menos que o ponto decimal esteja na primeira posição
	while decimal_pos > 1 and mantissa:sub(1,1) == '0' do
		mantissa = mantissa:sub(2)
		decimal_pos = decimal_pos - 1
	end
	-- Desloca o ponto decimal à direita para expoente > 0
	while exponent > 0 do
		decimal_pos = decimal_pos + 1
		exponent = exponent - 1
		if decimal_pos > #mantissa + 1 then mantissa = mantissa .. '0' end
		-- Remove os zeros à esquerda, a menos que o ponto decimal esteja na primeira posição
		while decimal_pos > 1 and mantissa:sub(1,1) == '0' do
			mantissa = mantissa:sub(2)
			decimal_pos = decimal_pos - 1
		end
	end
	-- Desloca o ponto decimal à direita para expoente < 0
	while exponent < 0 do
		if decimal_pos == 1 then
			mantissa = '0' .. mantissa
		else
			decimal_pos = decimal_pos - 1
		end
		exponent = exponent + 1
	end

	-- Insere o ponto decimal na posição correta e retorna
	return (negative and '-' or '') .. mantissa:sub(1, decimal_pos - 1) .. '' .. mantissa:sub(decimal_pos)
end

-- Arredonda um número para o número inteiro mais próximo (não usado)
local function round_num(x)
	if x%1 >= 0.5 then
		return math.ceil(x)
	else
		return math.floor(x)
	end
end

-- Arredonda um número para o número de duas palavras mais próximo (arredondamento = para cima, para baixo ou "on" para arredondar para o mais próximo).
-- Números com dois dígitos antes do decimal serão arredondados para um inteiro conforme especificado por round.
-- Números maiores serão arredondados para um número com apenas um dígito diferente de zero na frente e todos os outros dígitos zero.
-- O sinal negativo é preservado e não conta para o limite de palavras.
local function round_for_portuguese(num, round)
	-- Se for um número inteiro com no máximo dois dígitos, apenas retorna
	if num:find("^%-?%d?%d%.?$") then return num end

	local negative = num:find("^%-")
	if negative then
		-- We're rounding magnitude so flip it
		if round == 'up' then round = 'down' elseif round == 'down' then round = 'up' end
	end

	-- If at most two digits before decimal, round to integer and return
	local _, _, small_int, trailing_digits, round_digit = num:find("^%-?(%d?%d?)%.((%d)%d*)$")
	if small_int then
		if small_int == '' then small_int = '0' end
		if (round == 'up' and trailing_digits:find('[1-9]')) or (round == 'on' and tonumber(round_digit) >= 5) then
			small_int = tostring(tonumber(small_int) + 1)
		end
		return (negative and '-' or '') .. small_int
	end

	-- Ao arredondar para cima, qualquer número com > 1 dígito diferente de zero será arredondado para cima (por exemplo, 1000000,001 arredonda para 2000000)
	local nonzero_digits = 0
	for digit in num:gfind("[1-9]") do
		nonzero_digits = nonzero_digits + 1
	end

	num = num:gsub("%.%d*$", "") -- Remove a parte decimal
	-- Segundo dígito usado para determinar como arredondar o dígito inicial
	local _, _, lead_digit, round_digit, round_digit_2, rest = num:find("^%-?(%d)(%d)(%d)(%d*)$")
	if tonumber(lead_digit .. round_digit) < 20 and (1 + #rest) % 3 == 0 then
		-- Em português, números < 20 são uma palavra, então coloca 2 dígitos na frente e arredonda com base no 3º
		lead_digit = lead_digit .. round_digit
		round_digit = round_digit_2
	else
		rest = round_digit_2 .. rest
	end

	if (round == 'up' and nonzero_digits > 1) or (round == 'on' and tonumber(round_digit) >= 5) then
		lead_digit = tostring(tonumber(lead_digit) + 1)
	end
	-- Todos os dígitos, exceto o dígito inicial, serão reduzidos a zero
	rest = rest:gsub("%d", "0")
	return (negative and '-' or '') .. lead_digit .. '0' .. rest
end

local denominators = {
	[2] = { 'meio', plural = 'meios' },
	[3] = { 'terço' },
	[4] = { 'quarto', pt = 'quarto' },
	[5] = { 'quinto' },
	[6] = { 'sexto' },
	[8] = { 'oitavo' },
	[9] = { 'nono' },
	[10] = { 'décimo' },
	[16] = { 'dezesseis avos' },
}

-- Return status, fraction where:
-- status is a string:
--     "finished" if there is a fraction with no whole number;
--     "ok" if fraction is empty or valid;
--     "unsupported" if bad fraction;
-- fraction is a string giving (numerator / denominator) as portuguese text, or is "".
-- Only unsigned fractions with a very limited range of values are supported,
-- except that if whole is empty, the numerator can use "-" to indicate negative.
-- whole (string or nil): nil or "" if no number before the fraction
-- numerator (string or nil): numerator, if any (default = 1 if a denominator is given)
-- denominator (string or nil): denominator, if any
-- sp_pt (boolean): true if sp=pt
-- negative_word (string): word to use for negative sign, if whole is empty
-- use_one (boolean): false: 2+1/2 → "two and a half"; true: "two and one-half"
local function fraction_to_portuguese(whole, numerator, denominator, sp_pt, negative_word, use_one)
	if numerator or denominator then
		local finished = (whole == nil or whole == '')
		local sign = ''
		if numerator then
			if finished and numerator:sub(1, 1) == '-' then
				numerator = numerator:sub(2)
				sign = negative_word .. ' '
			end
		else
			numerator = '1'
		end
		if not numerator:match('^%d+$') or not denominator or not denominator:match('^%d+$') then
			return 'sem suporte', ''
		end
		numerator = tonumber(numerator)
		denominator = tonumber(denominator)
		local dendata = denominators[denominator]
		if not (dendata and 1 <= numerator and numerator <= 99) then
			return 'sem suporte', ''
		end
		local numstr, denstr
		local sep = '-'
		if numerator == 1 then
			denstr = sp_pt and dendata.pt or dendata[1]
			if finished or use_one then
				numstr = 'one'
			elseif denstr:match('^[aeiou]') then
				numstr = 'an'
				sep = ' '
			else
				numstr = 'a'
				sep = ' '
			end
		else
			numstr = numeral_to_portuguese_less_100(numerator)
			denstr = dendata.plural
			if not denstr then
				denstr = (sp_pt and dendata.pt or dendata[1]) .. 's'
			end
		end
		if finished then
			return 'finalizado', sign .. numstr .. sep .. denstr
		end
		return 'ok', ' e ' .. numstr .. sep .. denstr
	end
	return 'ok', ''
end

-- Pega um número decimal e o converte em texto em português.
-- Retorna nil se uma fração não puder ser convertida (somente alguns números são suportados para frações).
-- num (string ou nil): o número para converter.
--      Pode ser um decimal arbitrariamente grande, como "-123456789123456789.345", e
--      pode usar notação científica (por exemplo, "1.23E5").
--      Pode falhar para números muito grandes não listados em "groups", como "1E4000".
--      num é nil se não houver um número inteiro antes de uma fração.
-- numerator (string ou nil): numerador da fração (nil se não houver fração)
-- denominator (string or nil): denominador da fração (nil se não houver fração)
-- capitalize (booleano): se o resultado deve ser capitalizado (por exemplo, 'Um' em vez de 'um')
-- use_and (booleano): se deve usar a palavra 'e' entre a casa das dezenas/unidades e as casas mais altas
-- hyphenate (booleano): se deve hifenizar todas as palavras no resultado, útil como um adjetivo
-- ordinal (booleano): se deve produzir um ordinal (por exemplo, 'primeiro' em vez de 'um')
-- plural (booleano): se deve pluralizar o número resultante
-- links: nil: não adiciona nenhum link; 'on': vincula "bilhões" e maiores ao artigo Ordens de magnitude;
--        qualquer outro texto: lista de números para vincular (por exemplo, "bilhão,quatrilhão")
-- negative_word: palavra a ser usada para sinal negativo (normalmente 'negativo' ou 'menos'; nil para usar o padrão)
-- round: nil ou '': sem arredondamento; 'on': arredonda para o número de duas palavras mais próximo; 'up'/'down': arredonda para cima/para baixo para um número de duas palavras
-- zero: palavra a ser usada para o valor '0' (nil para usar o padrão)
-- use_one (booleano): false: 2+1/2 → "two and a half"; true: "two and one-half"
local function _numeral_to_portuguese(num, numerator, denominator, capitalize, use_and, hyphenate, ordinal, plural, links, negative_word, round, zero, use_one)
	if not negative_word then
		if use_and then
			-- TODO Should 'minus' be used when do not have sp=pt?
			--      If so, need to update testcases, and need to fix "minus zero".
			-- negative_word = 'minus'
			negative_word = 'negative'
		else
			negative_word = 'negative'
		end
	end
	local status, fraction_text = fraction_to_portuguese(num, numerator, denominator, not use_and, negative_word, use_one)
	if status == 'sem suporte' then
		return nil
	end
	if status == 'finalizado' then
		-- Input is a fraction with no whole number.
		-- Hack to avoid executing stuff that depends on num being a number.
		local s = fraction_text
		if hyphenate then s = s:gsub("%s", "-") end
		if capitalize then s = s:gsub("^%l", string.upper) end
		return s
	end
	num = scientific_notation_to_decimal(num)
	if round and round ~= '' then
		if round ~= 'on' and round ~= 'up' and round ~= 'down' then
			error("Modo de arredondamento inválido")
		end
		num = round_for_portuguese(num, round)
	end

	-- Separate into negative sign, num (digits before decimal), decimal_places (digits after decimal)
	local MINUS = '−'  -- Unicode U+2212 MINUS SIGN (may be in values from [[Module:Convert]])
	if num:sub(1, #MINUS) == MINUS then
		num = '-' .. num:sub(#MINUS + 1)  -- replace MINUS with '-'
	elseif num:sub(1, 1) == '+' then
		num = num:sub(2)  -- ignore any '+'
	end
	local negative = num:find("^%-")
	local decimal_places, subs = num:gsub("^%-?%d*%.(%d+)$", "%1")
	if subs == 0 then decimal_places = nil end
	num, subs = num:gsub("^%-?(%d*)%.?%d*$", "%1")
	if num == '' and decimal_places then num = '0' end
	if subs == 0 or num == '' then error("Número decimal inválido") end

	-- Para cada grupo de 3 dígitos, exceto o último, imprime com o nome do grupo apropriado (por exemplo, milhão)
	local s = ''
	while #num > 3 do
		if s ~= '' then s = s .. ' ' end
		local group_num = math.floor((#num - 1) / 3)
		local group = groups[group_num]
		local group_digits = #num - group_num*3
		s = s .. numeral_to_portuguese_less_1000(num:sub(1, group_digits), false, false, false, zero) .. ' '
		if links and (((links == 'on' and group_num >= 3) or links:find(group)) and group_num <= 13) then
			s = s .. '[[Orders_of_magnitude_(numbers)#10' .. group_num*3 .. '|' .. group .. ']]'
		else
			s = s .. group
		end
		num = num:sub(1 + group_digits)
		num = num:gsub("^0*", "")  -- Trim leading zeros
	end

	-- Handle final three digits of integer part
	if s ~= '' and num ~= '' then
		if #num <= 2 and use_and then
			s = s .. ' e '
		else
			s = s .. ' '
		end
	end
	if s == '' or num ~= '' then
		s = s .. numeral_to_portuguese_less_1000(num, use_and, ordinal, plural, zero)
	elseif ordinal or plural then
		-- Round numbers like "one million" take standard suffixes for ordinal/plural
		s = s .. standard_suffix(ordinal, plural)
	end

	-- For decimal places (if any) output "point" followed by spelling out digit by digit
	if decimal_places then
		s = s .. ' vírgula'
		for i = 1, #decimal_places do
			s = s .. ' ' .. ones_position[tonumber(decimal_places:sub(i,i))]
		end
	end

	s = s:gsub("^%s*(.-)%s*$", "%1")   -- Trim whitespace
	if ordinal and plural then s = s .. 's' end  -- s suffix works for all ordinals
	if negative and s ~= zero then s = negative_word .. ' ' .. s end
	s = s:gsub("negative zero", "zero")
	s = s .. fraction_text
	if hyphenate then s = s:gsub("%s", "-") end
	if capitalize then s = s:gsub("^%l", string.upper) end
	return s
end

local function _numeral_to_portuguese2(args)
	local num = tostring(args.num)

	num = num:gsub("^%s*(.-)%s*$", "%1")   -- Trim whitespace
	num = num:gsub(",", "")   -- Remove vírgulas
	num = num:gsub("^<span[^<>]*></span>", "") -- Generated by Template:Age
	if num ~= '' then  -- uma fração pode conter um número inteiro vazio
		if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then
			-- A entrada não está em um formato válido, tente avaliá-la como uma expr para ver
			-- se isso produz um número (por exemplo, "3 + 5" se tornará "8").
			local noerr, result = pcall(mw.ext.ParserFunctions.expr, num)
			if noerr then
				num = result
			end
		end
	end

	-- Chama a função auxiliar passando args
	return _numeral_to_portuguese(
		num,
		args['numerator'],
		args['denominator'],
		args['capitalize'],
		args['use_and'],
		args['hyphenate'],
		args['ordinal'],
		args['plural'],
		args['links'],
		args['negative_word'],
		args['round'],
		args['zero'],
		args['use_one']
	) or ''
end

local p = {  -- Funções que podem ser chamadas de outro módulo
	roman_to_numeral = roman_to_numeral,
	spell_number = _numeral_to_portuguese,
	spell_number2 = _numeral_to_portuguese2,
	portuguese_to_ordinal = portuguese_to_ordinal,
	portuguese_to_numeral = portuguese_to_numeral,
}

function p._roman_to_numeral(frame) -- Chamável via {{#invoke:ConvertNumeric|_roman_to_numeral|VI}}
	return roman_to_numeral(frame.args[1])
end

function p._portuguese_to_ordinal(frame) -- Chamável via {{#invoke:ConvertNumeric|_portuguese_to_ordinal|Primeiro}}
	return portuguese_to_ordinal(frame.args[1])
end

function p._portuguese_to_numeral(frame) -- Chamável via {{#invoke:ConvertNumeric|_portuguese_to_numeral|Um}}
	return portuguese_to_numeral(frame.args[1])
end

function p.numeral_to_portuguese(frame)
	local args = frame.args
	-- Chamada final para a função auxiliar passando args a partir do quadro
	return _numeral_to_portuguese2{
		['num'] = args[1],
		['numerator'] = args['numerator'],
		['denominator'] = args['denominator'],
		['capitalize'] = args['case'] == 'U' or args['case'] == 'u',
		['use_and'] = args['sp'] ~= 'pt',
		['hyphenate'] = args['adj'] == 'on',
		['ordinal'] = args['ord'] == 'on',
		['plural'] = args['pl'] == 'on',
		['links'] = args['lk'],
		['negative_word'] = args['negative'],
		['round'] = args['round'],
		['zero'] = args['zero'],
		['use_one'] = args['one'] == 'one'  -- experimento: usar '|one=um' faz com que a fração 2+1/2 dê "dois e um meio" em vez de "dois e meio"
	}
end

---- Função recursiva para p.decToHex
local function decToHexDigit(dec)
	local dig = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
	local div = math.floor(dec/16)
	local mod = dec-(16*div)
	if div >= 1 then return decToHexDigit(div)..dig[mod+1] else return dig[mod+1] end
end -- Eu acho que isso deveria ser feito com uma chamada final, mas primeiro eu quero algo que funcione

---- Localiza todos os números decimais no texto de entrada e transforma cada um deles em hexas
function p.decToHex(frame)
	local args=frame.args
	local parent=frame.getParent(frame)
	local pargs={}
	if parent then pargs=parent.args end
	local text=args[1] or pargs[1] or ""
	local minlength=args.minlength or pargs.minlength or 1
	minlength=tonumber(minlength)
	local prowl=mw.ustring.gmatch(text,"(.-)(%d+)")
	local output=""
	repeat
		local chaff,dec=prowl()
		if not(dec) then break end
		local hex=decToHexDigit(dec)
		while (mw.ustring.len(hex)<minlength) do hex="0"..hex end
		output=output..chaff..hex
	until false
	local chaff=mw.ustring.match(text,"(%D+)$") or ""
	return output..chaff
end

return p