Task

Show how to spell out a number in English.

You can use a preexisting implementation or roll your own, but you should support inputs up to at least one million (or the maximum value of your language's default bounded integer type, if that's less).

Support for inputs other than positive integers (like zero, negative integers, and floating-point numbers) is optional.



Related task

   Spelling of ordinal numbers.


 
global outnum$
dim ones$(20),tens$(9),gr$(5),group(5)
for a = 0 to 19
    read a$:ones$(a)=a$
next a
for a = 1 to 8
    read a$:tens$(a)=a$
next a
 
[start]
    redim gr$(5)
    redim group(5)
 
    input "Enter a number (nonzero positive integers only less than 1 quadrillion, no commas): ";num$
 
    num$=trim$(num$)
    numVal=int(val(num$))
    if numVal=0 or numVal>999999999999999 then print "Ended" : end
 
    numParse = numVal
    numLen = len(str$(numParse))
 
    if numLen<=15 then groupCount=5
    if numLen<=12 then groupCount=4
    if numLen<=9 then groupCount=3
    if numLen<=6 then groupCount=2
    if numLen<=3 then groupCount=1
 
    if numLen>12 and numLen<=15 then
        group(5)=int(numParse/1000000000000)
        call convert group(5)," trillion"
        gr$(5)=outnum$
        numParse=nP(numParse,12)
        numLen=len(str$(numParse))
    end if
 
    if numLen>9 and numLen<=12 then
        group(4)=int(numParse/1000000000)
        call convert group(4)," billion"
        gr$(4)=outnum$
        numParse=nP(numParse,9)
        numLen=len(str$(numParse))
    end if
 
    if numLen>6 and numLen<=9 then
        group(3)=int(numParse/1000000)
        call convert group(3)," million"
        gr$(3)=outnum$
        numParse=nP(numParse,6)
        numLen=len(str$(numParse))
    end if
 
    if numLen>3 and numLen<=6 then
        group(2)=int(numParse/1000)
        call convert group(2)," thousand"
        gr$(2)=outnum$
        numParse=nP(numParse,3)
        numLen=len(str$(numParse))
    end if
 
    if numLen<=3 then
        group(1)=numParse
        call convert group(1),""
        gr$(1)=outnum$
        numLen=len(str$(numParse))
    end if
 
    print
    for a=groupCount to 1 step -1
        print gr$(a);" ";
    next a
    print
    print
 
goto [start]
 
sub convert num, dem$
    outnum$=""
    for a=len(str$(num)) to 1 step -1
        b$=mid$(str$(num),a,1)
        c=val(b$)
        d=len(str$(num))-a+1
        select case
            case d=1
                outnum$=ones$(c)
            case d=2
                if c<2 then
                    outnum$=ones$(val(right$(str$(num),2)))
                else
                    if c>=2 and val(right$(str$(num),1))<>0 then
                        outnum$=tens$(c-1)+"-"+outnum$ 
                    else
                        outnum$=tens$(c-1)
                    end if
                end if
            case d=3
                if c<>0 then outnum$=ones$(c)+" hundred "+outnum$
        end select
    next a
    outnum$=outnum$+dem$
end sub
 
function nP(num,L)
    nP=val(right$(str$(num),L))
end function
 
data "","one","two","three","four","five","six","seven","eight","nine"
data "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"
data "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
 