Add terrible itoa
This commit is contained in:
parent
1a51fa131c
commit
a15d87343e
|
@ -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
|
Loading…
Reference in New Issue