Jump to content

Module:BaseConvert

विकिपीडिया से
imported>Toohool (Created page with '-- -- Converts numbers to a specified base between 2 and 36, for use in -- templates such as {{binary}}, {{octal}}, {{hexadecimal}}, etc. -- local p = {} funct...') के द्वारा 09:38, 22 फरवरी 2013 के बदलाव
(अंतर) ← पुरान बदलाव | हाल के संसोधन (अंतर) | नया बदलाव → (अंतर)
--
-- 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