29 lines
992 B
Plaintext
29 lines
992 B
Plaintext
singles = " one two three four five six seven eight nine ".split
|
|
teens = "ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen ".split
|
|
tys = " twenty thirty forty fifty sixty seventy eighty ninety".split
|
|
ions = "thousand million billion".split
|
|
|
|
numberName = function(n)
|
|
if n == 0 then return "zero"
|
|
a = abs(n)
|
|
r = "" // (result)
|
|
for u in ions
|
|
h = a % 100
|
|
if h > 0 and h < 10 then r = singles[h] + " " + r
|
|
if h > 9 and h < 20 then r = teens[h-10] + " " + r
|
|
if h > 19 and h < 100 then r = tys[h/10] + "-"*(h%10>0) + singles[h%10] + " " + r
|
|
h = floor((a % 1000) / 100)
|
|
if h then r = singles[h] + " hundred " + r
|
|
a = floor(a / 1000)
|
|
if a == 0 then break
|
|
if a % 1000 > 0 then r = u + " " + r
|
|
end for
|
|
if n < 0 then r = "negative " + r
|
|
return r
|
|
end function
|
|
|
|
// Test cases:
|
|
for n in [-1234, 0, 7, 42, 4325, 1000004, 214837564]
|
|
print n + ": " + numberName(n)
|
|
end for
|