Preskočiť na obsah

Modul:ConvertNumeric

z Wikipédie, slobodnej encyklopédie


Túto dokumentáciu [upraviť] [história] [obnoviť]
Dokumentácia Dokumentácia

Použitie

[upraviť zdroj]
{{#invoke:ConvertNumeric|numeral_to_slovak|číslo}}

Príklad použitia

[upraviť zdroj]

{{#invoke:ConvertNumeric|numeral_to_slovak|55}} sa rovná päťdesiatpäť
{{#invoke:ConvertNumeric|numeral_to_slovak|413}} sa rovná štyristotrinásť



Ak máte otázku k tomuto modulu, alebo potrebujete jeho rozšírenie a neviete ho správne naformátovať, pýtajte sa v diskusii k modulu. Pokiaľ je potrebné modul urgentne opraviť, obráťte sa na technickú podporu.

local ones_position = {
	[0] = 'nula',
	[1] = 'jeden',
	[2] = 'dva',
	[3] = 'tri',
	[4] = 'štyri',
	[5] = 'päť',
	[6] = 'šesť',
	[7] = 'sedem',
	[8] = 'osem',
	[9] = 'deväť',
	[10] = 'desať',
	[11] = 'jdenásť',
	[12] = 'dvanásť',
	[13] = 'trinásť',
	[14] = 'štrnásť',
	[15] = 'pätnásť',
	[16] = 'šestnásť',
	[17] = 'sedemnásť',
	[18] = 'osemnásť',
	[19] = 'devätnásť'
}

local tens_position = {
	[2] = 'dvadsať',
	[3] = 'tridsať',
	[4] = 'štyridsať',
	[5] = 'päťdesiat',
	[6] = 'šesťdesiat',
	[7] = 'sedemdesiat',
	[8] = 'osemdesiat',
	[9] = 'deväťdesiat'
}

local groups = {
	[1] = 'tisíc',
	[2] = 'milión',
	[3] = 'miliarda',
	[4] = 'bilión',
	[5] = 'biliarda',
	[6] = 'trilión',
	[7] = 'triliarda'
}

-- Converts a given integer between 0 and 100 to English text (e.g. 47 -> forty-seven)
local function numeral_to_slovak_less_100(num)
	local terminal_ones, terminal_tens

	terminal_ones = ones_position
	terminal_tens = tens_position
	
	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)] .. terminal_ones[num % 10]
	end
end

-- Converts a given integer (in string form) between 0 and 1000 to English text (e.g. 47 -> forty-seven)
local function numeral_to_slovak_less_1000(num)
    local num = tonumber(num)
	if num < 100 then
		return numeral_to_slovak_less_100(num)
	elseif num % 100 == 0 then
		if num == 100 then
			return 'sto'
		else
			return ones_position[num/100] .. 'sto'
		end
	else
		if num > 100 and num < 200 then
			return 'sto' .. numeral_to_slovak_less_100(num % 100)
		else
			return ones_position[math.floor(num/100)] .. 'sto' .. numeral_to_slovak_less_100(num % 100)
		end
	end
end

local function _numeral_to_slovak(number)
	local num = tostring(number)
	-- For each group of 3 digits except the last one, print with appropriate group name (e.g. million)
	local s = ''
	while #num > 3 do
		if s ~= '' then s = s .. ' ' end
		local group_num = ((#num - 1) / 3)
		local group = groups[group_num]
		local group_digits = #num - group_num*3
		str = tostring(num):sub(1, group_digits)
		if group_digits > 6 then
			s = s .. numeral_to_slovak_less_1000(tonumber(str)) .. ' '
			s = s .. groups[group_num]
			else
			s = s .. numeral_to_slovak_less_1000(tonumber(str))
			s = s .. groups[group_num]
		end
		
		num = tostring(num)
		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 then
			s = s .. ' '
		end
	end
	if s == '' or num ~= '' then
		s = s .. numeral_to_slovak_less_1000(tonumber(num))
	end

	s = s:gsub("^%s*(.-)%s*$", "%1")   -- Trim whitespace
	return s
end

local p = {numeral_to_slovak = numeral_to_slovak}

function p.numeral_to_slovak(frame)

    local num = frame.args[1]
	num = num:gsub("^%s*(.-)%s*$", "%1")   -- Trim whitespace
	num = num:gsub(",", "")   -- Remove commas
	num = num:gsub("^<span[^<>]*></span>", "") -- Generated by Template:age

	num = tonumber(num)
    if num == nil then
    	return '<strong class="error">Error: input not recognized as a number</strong>'
    end
    
    num = math.floor(num)
    if num < 1 or num > 1000000000000000000000 then
        return '<strong class="error">Error: ' .. num .. ' out of range</strong>'
    end

	return _numeral_to_slovak(num) or ''
end

return p