Number_to_String

Here is the code that takes an integer and spells it out in plain English. It was written by Henry on JB forum, in [|this thread] I'm pretty sure he would allow for it to be put here: tsh73: >Henry, >your code is clean and nice (and works, too). >And task it solves just pops out now and then. >Would you mind if I post this code to JB Wiki, shared code section? With full credits of course. Henry: >Thanks Tsh. >I'd sure like to see the wiki more active. That's a very good idea. >(...)

sample output: code Converting numbers to words ... Enter an integer (-1 to exit) : 0 zero Enter an integer (-1 to exit) : 123 one hundred twenty three Enter an integer (-1 to exit) : 132432 one hundred thirty two thousand four hundred thirty two Enter an integer (-1 to exit) : -200 negative two hundred code

code format="thinbasic" ' Convert numbers to text ' written by Henry, Nov 2008 ' Set up our string arrays. Load them using For/Next loops Dim ones$(20),tens$(10),groups$(6) Data "",one,two,three,four,five,six,seven,eight,nine,ten,eleven Data twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen Data ten,twenty,thirty,fourty,fifty,sixty,seventy,eighty,ninety Data "",thousand,million,billion,trillion,quadrillion For a = 0 to 19 : read b$ : ones$(a) = b$ : next a For a = 1 to 9  : read b$ : tens$(a) = b$ : next a  For a = 1 to 6  : read b$ : group$(a)= b$ : next a

print "Converting numbers to words ..." Do   Input "Enter an integer (-1 to exit) : "; n    n2 = Len(Str$(Abs(Int(n))))  ' n2 = digits before decimal point Select Case Case n<>Int(n) Print "Error -- integers only!" Case n2>18 print "Number too large. Try again." Case Else print num2text$(n) End Select Loop Until n=-1 print print "Done." End ' *** End of Main Program ***

' num2text$ -- convert a number into words ' negative numbers are handled by a recursive call ' the number is broken down into groups: trillion, billion, etc Function num2text$(num) if num<0 then num2text$ = "negative "; num2text$(abs(num)) : exit function if num=0 then num2text$ = "zero" : exit function n2t$ = "" mag = Len(Str$(Int(num))) ' the magnitude of our number pow = 3*int((mag-1)/3)   ' 3 * our group

' In this loop, we grab the next 3-digit group "num3" ' convert it, add the group$ (billions, millions, etc) ' reduce "num" by the amount converted ' and adjust "pow" to get ready for the next group While num>0 num3 = Int( num / (10^pow) ) ' grab next 3-digit group if num3>0 then n2t$ = n2t$; convert3$(num3); group$(int(pow/3)+1);" " end if   num = num - num3*(10^pow) pow = pow - 3 Wend num2text$ = n2t$ End Function ' num2text

' convert3$ -- convert a 3-digit number to text ' We break down "num3" into digits, convert them to words ' using the string arrays: ones$, tens$ ' Then we remove double spaces from the string. Function convert3$(num3) num3$ = "" d1 = int(num3/100) d2 = int((num3 mod 100)/10) d3 = num3 mod 10 if d1>0 then num3$ = ones$(d1); " hundred " if d2>1 then num3$ = num3$ + tens$(d2);" ";ones$(d3);" " else num3$ = num3$ + ones$(d2*10+d3); " " end if pos = instr(num3$, "  ", 1) while pos>0 num3$ = left$(num3$, pos)+mid$(num3$,pos+2) pos = instr(num3$, " ",pos) wend convert3$ = num3$ End Function ' convert3 code