Module:tengwar/numbers

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}

function basen(n,b)
    n = math.floor(n)
    if not b or b == 10 then return tostring(n) end
    local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    local t = {}
    local sign = ""
    if n < 0 then
        sign = "-"
    n = -n
    end
    repeat
        local d = (n % b) + 1
        n = math.floor(n / b)
        table.insert(t, 1, digits:sub(d,d))
    until n == 0
    return sign .. table.concat(t,"")
end

function export.ar2teng(number, base, font)
	local digits = {}
	local decmark, dozmark, dozlast
	if font == 'annatar' or not font then
		digits = {'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û'}
		digits[0] = 'ð'
		decmark = 'T'
		dozmark = 'Ê'
		dozlast = '™'
	elseif font == 'telcontar' then
		digits = {'', '', '', '', '', '', '', '', '', '', ''}
		digits[0] = ''
		decmark = ''
		dozmark = ''
		dozlast = ''
	else
		error ('invalid font, must be annatar or telcontar')
	end
	
	if base == 10 or not base then
		if key < 10 then
			number = digits[key]
		else
			local outp = {}
			for i = 0, #number do
		    	local pos = #number + 1 - i
		    	table.insert(outp, digits[tonumber(string.sub(number, pos, pos))]..decmark)
			end
			number = table.concat(outp)
		end
	elseif base == 12 then
		if key < 12 then
			number = digits[key]
		else
			number = basen(number, 12)
			local outp = {}
			for i = 0, #number do
		    	local pos = #number + 1 - i
		    	local cdig = string.gsub(string.gsub(string.sub(number, pos, pos), 'A', '10'), 'B', '11')
		    	if (i == #number) then
		    		table.insert(outp, digits[tonumber(cdig)]..dozlast)
		    	else
		    		table.insert(outp, digits[tonumber(cdig)]..dozmark)
		    	end
			end
			number = table.concat(outp)
		end
	else
		error ('invalid base, must be 10 or 12')
	end

	return number
end
return export