workshit/src/itoa.moon

27 lines
421 B
Plaintext

LOW_BAR = 48 -- '0' in ascii
-- number -> string
itoa = (n) ->
with ret = ""
negative = false
if n < 0
negative = true
n = n * -1
n = math.floor n -- no decimals.
while true
myn = n % 10
ret = string.char(myn + LOW_BAR) .. ret
n = math.floor(n / 10)
break if n == 0
ret = "-" .. ret if negative
print itoa 42
print itoa 420
print itoa 4200
print itoa -999