Add terrible itoa

This commit is contained in:
Christine Dodrill 2015-06-25 17:13:05 -07:00
parent 1a51fa131c
commit a15d87343e
1 changed files with 26 additions and 0 deletions

26
src/itoa.moon Normal file
View File

@ -0,0 +1,26 @@
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