Module:number base: Difference between revisions

From Linguifex
Jump to navigation Jump to search
(Created page with "local export = {} local floor, insert = math.floor, table.insert function export.base12(n) n = floor(tonumber(n)) local digits = "0123456789¹²" local t = {} local sign = "" repeat local d = (n % 12) + 1 n = floor(n / 12) insert(t, 1, digits:sub(d,d)) until n == 0 return sign .. table.concat(t,"") end return export")
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 3: Line 3:
local floor, insert = math.floor, table.insert
local floor, insert = math.floor, table.insert


-- Ross Berteig on SO: https://stackoverflow.com/a/3554821
function export.base12(n)
function export.base12(n)
     n = floor(tonumber(n))
     n = floor(tonumber(n))
     local digits = "0123456789¹²"
     local digits = "0123456789AB"
     local t = {}
     local t = {}
    local sign = ""
     repeat
     repeat
         local d = (n % 12) + 1
         local d = (n % 12) + 1
Line 13: Line 13:
         insert(t, 1, digits:sub(d,d))
         insert(t, 1, digits:sub(d,d))
     until n == 0
     until n == 0
     return sign .. table.concat(t,"")
     return table.concat(t)
end
end


return export
return export

Latest revision as of 19:16, 6 July 2023

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

local export = {}

local floor, insert = math.floor, table.insert

-- Ross Berteig on SO: https://stackoverflow.com/a/3554821
function export.base12(n)
    n = floor(tonumber(n))
    local digits = "0123456789AB"
    local t = {}
    repeat
        local d = (n % 12) + 1
        n = floor(n / 12)
        insert(t, 1, digits:sub(d,d))
    until n == 0
    return table.concat(t)
end

return export