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 |
local result = "" |
||
repeat |
|||
digit = n % base |
local digit = n % base |
||
if digit > 9 then |
if digit > 9 then |
||
digit = string.char(digit + 87) |
|||
⚫ | |||
result = digit .. result |
result = digit .. result |
||
n = n // base |
|||
⚫ | |||
until n == 0 |
|||
return result |
return result |
||
end |
end |