Модуль:ЛічбыПропісам

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі

Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:ЛічбыПропісам/Дакумэнтацыя

-- This module converts a number into its written English form.
-- For example, "2" becomes "two", and "79" becomes "seventy-nine".

local getArgs = require('Модуль:Аргумэнты').getArgs

local p = {}

local max = 100 -- The maximum number that can be parsed.

local ones = {
	[0] = 'нуль',
	[1] = 'адзін',
	[2] = 'два',
	[3] = 'тры',
	[4] = 'чатыры',
	[5] = 'пяць',
	[6] = 'шэсьць',
	[7] = 'сем',
	[8] = 'восем',
	[9] = 'дзевяць'
}

local specials = {
	[10] = 'дзесяць',
	[11] = 'адзінаццаць',
	[12] = 'дванаццаць',
	[13] = 'трынаццаць',
	[15] = 'пятнаццаць',
	[18] = 'васемнаццаць',
	[20] = 'дваццаць',
	[30] = 'трыццаць',
	[40] = 'сорак',
	[50] = 'пяцьдзясят',
	[60] = 'шэсьцьдзесят',
	[70] = 'семдзесят',
	[80] = 'восемдзесят',
	[90] = 'дзевяноста',
	[100] = 'сто'
}

local formatRules = {
	{num = 90, rule = 'дзевяноста-%s'},
	{num = 80, rule = 'восемдзесят-%s'},
	{num = 70, rule = 'семдзесят-%s'},
	{num = 60, rule = 'шэсьцьдзесят-%s'},
	{num = 50, rule = 'пяцьдзясят-%s'},
	{num = 40, rule = 'сорак-%s'},
	{num = 30, rule = 'трыццаць-%s'},
	{num = 20, rule = 'дваццаць-%s'},
	{num = 10, rule = '%sнаццаць'}
}

function p.main(frame)
	local args = getArgs(frame)
	local num = tonumber(args[1])
	local success, result = pcall(p._main, num)
	if success then
		return result
	else
		return string.format('<strong class="error">Памылка: %s</strong>', result) -- "result" is the error message.
	end
	return p._main(num)
end

function p._main(num)
	if type(num) ~= 'number' or math.floor(num) ~= num or num < 0 or num > max then
		error('павінен быць цэлы лік між 0 і ' .. tostring(max), 2)
	end
	-- Check for numbers from 0 to 9.
	local onesVal = ones[num]
	if onesVal then
		return onesVal
	end
	-- Check for special numbers.
	local specialVal = specials[num]
	if specialVal then
		return specialVal
	end
	-- Construct the number from its format rule.
	onesVal = ones[num % 10]
	if not onesVal then
		error('Неспадзяваная памылка разбору ўводу ' .. tostring(num))
	end
	for i, t in ipairs(formatRules) do
		if num >= t.num then
			return string.format(t.rule, onesVal)
		end
	end
	error('Ня знойдзенае правіла фарматаваньня для значэньня ' .. tostring(num))
end

return p