diff --git a/src/itoa.moon b/src/itoa.moon new file mode 100644 index 0000000..1ed9b24 --- /dev/null +++ b/src/itoa.moon @@ -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