Gaa na ọdịnaya

Module:BaseConvert

Shí Wikipedia, njikotá édémédé nke onyobulạ
Mmeghari kemgbe 09:38, 22 Febụwarị 2013 si n'aka Toohool (ṅkátá | mmetara)
(ndịịche) ← Orübà di kwa nke ichié | Orübà nke ubwa (ndịịche) | Orübà di kwa ohúrù → (ndịịche)

Documentation for this module may be created at Module:BaseConvert/doc

--
-- Converts numbers to a specified base between 2 and 36, for use in
-- templates such as {{binary}}, {{octal}}, {{hexadecimal}}, etc.
--

local p = {}

function p._convert(n, base)
    local digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    n = math.floor(n)
    local sign = ''
    if n < 0 then
        sign = '-'
        n = -n
    end
    
    local t = {}
    repeat
        local d = (n % base) + 1
        n = math.floor(n / base)
        table.insert(t, 1, digits:sub(d, d))
    until n == 0
    
    return sign .. table.concat(t, '')
end

function p.convert(frame)
    local n = frame.args.n
    local base = frame.args.base
    return p._convert(n, base)
end

return p