In your example of 1550, you say "five hundred AND fifty". Technically, you only say 'and' when referring to the decimal or fraction. I had a math teacher that used to yell at us for that.
I thought this made a neat little challenge, so I had a go at it myself.
I'm not trying to show up your code or anything, just thought tackling it myself from another approach would be fun.
I ran a speed test, mainly to see if the extra overhead of using my 4 arrays was worth it. I ran a loop calling the function from 1 to 9999 (cause you said yours wouldn't do 10k and up). Mine completes in 50ms. I'm afraid to say yours was, well, a tad higher. Yours starts out just fine, but I found the larger the number the longer it takes. 1 through 99 took only 16ms, but 8999 (just a single number, not a range) took 37ms. And 9999 itself took 42ms.
I'm looking at your code now to see if I can tell why larger numbers have such a higher latency, but I bet it has something to do with the while loops.
dim num$(32)
restore _NumData
for i = 1 to 9 : read num$(i) : next i
for i = 0 to 9 : read num$(i+10) : next i
for i = 1 to 8 : read num$(i+19) : next i
for i = 1 to 4 : read num$(i+28) : next i
REM ============ START DEMO =============
disable escapekey
r = 0
repeat
cls
if spacekey() = 1 and flag = 0
flag = 1
r = rnd(99999)
endif
if spacekey() = 0 then flag = 0
getTextNumber$(r)
until escapekey()
end
REM ============= END DEMO =============
REM ====================================
REM Returns the string representation of
REM a number up to 999 trillion
REM ====================================
function getTextNumber$(x)
if x = 0 then exitfunction "zero"
n$ = str$(x) : L = len(n$)
label = 0 : w$ = ""
for i = L to 1 step -3
w$ = getNumberSegment(getStringSegment$(n$, i-2, i))+" "+num$(label+28)+" "+w$
inc label
next i
print x
print w$
endfunction w$
REM ====================================
REM Returns the string segment between
REM start position and ending position
REM ====================================
function getStringSegment$(t$, start, finish)
t$ = left$(t$, finish)
t$ = right$(t$, (len(t$)-start+1))
endfunction t$
REM ====================================
REM Returns the string representation of
REM a 1 to 3 digit number
REM ====================================
function getNumberSegment(seg$)
i = 1
d$ = mid$(seg$, i) : inc i
d = val(d$)
L = len(seg$)
a$ = "" : b$ = "" : c$ = ""
if L = 3
if d > 0 then a$ = num$(d) + " hundred"
d = val(mid$(seg$, i)) : inc i
endif
if L > 1
if d > 0
if d > 1
b$ = num$(d+18)
d = val(mid$(seg$, i))
if d > 0 then c$ = num$(d)
else
if d = 1
d = val(mid$(seg$, i))
b$ = num$(d+10)
endif
endif
else
d = val(mid$(seg$, i))
if d > 0 then c$ = num$(d)
endif
else
c$ = num$(d)
endif
rem so we have the proper spaces in the final word
word$ = a$
if word$ <> "" and b$ <> "" then word$ = word$ + " "
word$ = word$ + b$
if word$ <> "" and c$ <> "" then word$ = word$ + " "
word$ = word$ + c$
endfunction word$
_NumData:
data "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
data "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen"
data "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
data "thousand", "million", "billion", "trillion"
"You're not going crazy. You're going sane in a crazy world!" ~Tick