Ugrás a tartalomhoz

Szerkesztő:Tilar Vehulor/Lua/Szám kiírva

A Wikipédiából, a szabad enciklopédiából

A Modul:Szám kiírva átrendezett, olvashatóbbá tett változata.

local p = {}


-- dictionary
local ones_names = { 'egy', 'két', 'három', 'négy', 'öt', 'hat', 'hét', 'nyolc', 'kilenc' }
local tens_names = { 'tíz', 'húsz', 'harminc', 'negyven', 'ötven', 'hatvan', 'hetven', 'nyolcvan', 'kilencven' }


-- is_last: if there is an other word after this number,
-- you can use 'két', but you must use 'kettő' otherwise
local function group_to_string(number, is_last)
	
	local ones = number % 10
	local tens = (number - ones) % 100
	local hundreds = number - (ones + tens)
	
	-- hundreds
	local hundreds_text = ''
	if hundreds == 0 then
		hundreds_text = ''
	elseif hundreds == 100 then
		hundreds_text = 'száz'
	else
		hundreds_text = ones_names[hundreds/100] .. 'száz'
	end
	
	-- tens
	local tens_text = ''
	if tens == 0 then
		tens_text = ''
	elseif tens == 10 then
		if ones == 0 then
			tens_text = 'tíz'
		else
			tens_text = 'tizen'
		end
	elseif tens == 20 then
		if ones == 0 then
			tens_text = 'húsz'
		else
			tens_text = 'huszon'
		end
	else
		tens_text = tens_names[tens/10]
	end
	
	-- ones
	local ones_text = ''
	if ones == 0 then
		ones_text = ''
	elseif ones == 2 and last then
		ones_text = 'kettő'
	else
		ones_text = ones_names[ones]
	end
	
	return hundreds_text .. tens_text .. ones_text
end


local function number_to_string(number)
	
	-- if number is negative or is not (close to) an integer or is too big: return empty string
	if number < 0 or number >= 1E12 or math.abs(number - math.floor(number)) > 1E-12 then
		return ''
	end
	
	if number == 0 then
		return 'nulla'
	end
	
	if number < 1000 then
		return group_to_string(number, true)
	elseif number < 2000 then
		return 'ezer' .. group_to_string(number % 1000, true)
	else
		local groups = {}
		
		local ones = number % 1000
		local thousands = (number - ones) % 1E6
		local millions = (number - (ones + thousands)) % 1E9
		local billions = number - (ones + thousands + millions) % 1E12
		
		if billions > 0 then
			table.insert(groups, group_to_string(billions / 1E9) .. 'milliárd')
		end
		if millions > 0 then
			table.insert(groups, group_to_string(millions / 1E6) .. 'millió')
		end
		if thousands > 0 then
			table.insert(groups, group_to_string(thousands / 1000) .. 'ezer')
		end
		if ones > 0 then
			table.insert(groups, group_to_string(ones, true))
		end
		return table.concat(groups, '-')
	end
	
	return ''
end


function p._main(number)
	
	-- validation
	if not number or not tonumber(number) then
		return ''
	end
	
	return number_to_string(tonumber(number))
end


function p.main(frame)
	return p._main(require('Modul:Arguments').getArgs(frame)[1])
end


return p