Non-decimal radices/Convert: Difference between revisions

Content added Content deleted
(support 0, fix non-exhaustive matching, use modern API)
(Lua: fix case of n = 0)
Line 2,252: Line 2,252:
Only had to write 'dec2base' as the reverse is provided by the in-built function 'tonumber'
Only had to write 'dec2base' as the reverse is provided by the in-built function 'tonumber'
<syntaxhighlight lang="lua">function dec2base (base, n)
<syntaxhighlight lang="lua">function dec2base (base, n)
local result, digit = ""
local result = ""
while n > 0 do
repeat
digit = n % base
local digit = n % base
if digit > 9 then digit = string.char(digit + 87) end
if digit > 9 then
n = math.floor(n / base)
digit = string.char(digit + 87)
end
result = digit .. result
result = digit .. result
n = n // base
end
until n == 0
return result
return result
end
end