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.


dim shared as string*9 lows(0 to 19) = {"", "one", "two", "three", "four",_
    "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",_
    "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",_
    "eighteen", "nineteen"}
dim shared as string*7 tens(0 to 9) = {"", "", "twenty", "thirty", "forty",_
    "fifty", "sixty", "seventy", "eighty", "ninety"}
dim shared as string*8 lev(0 to 3) = {"", "thousand", "million", "billion"}
 
function numname_int( byval n as integer ) as string
    if n = 0 then return "zero"
    if n < 0 then return "negative " + numname_int( -n )
    dim as integer t = -1, lasttwo, hundreds
    redim as integer triples(0 to 0)
    dim as string ret, tripname
    while n > 0
        t += 1
        triples(t) = n mod 1000
        n \= 1000
    wend
    for i as integer = t to 0 step -1
        tripname = ""
        if triples(i) = 0 then continue for
        lasttwo = triples(i) mod 100
        hundreds = triples(i)\100
        if lasttwo < 20 then 
            tripname = lows(lasttwo) + tripname + " "
        else
            tripname = tens(lasttwo\10) + "-" + lows(lasttwo mod 10) + " " + tripname
        end if
        if hundreds > 0 then 
             if lasttwo > 0 then tripname = " and " + tripname
             tripname = lows(hundreds)+" hundred" + tripname
        end if
        if i=0 and t>0 and hundreds = 0 then tripname = " and " + tripname
        tripname += lev(i)+" "
        ret = ltrim(ret) + ltrim(tripname)
    next i
    return ltrim(rtrim(ret))
end function
 
function numname( n as double ) as string
    if n=int(n) then return numname_int(int(n))
    dim as string prefix = numname_int( int(abs(n)) )+" point "
    dim as string decdig = str(abs(n)-int(abs(n))), ret
    if n < 0 then prefix = "negative "+prefix
    ret = prefix
    for i as uinteger = 3 to len(decdig)
        ret = ret + numname(val(mid(decdig,i,1)))+" "
    next i
    return ltrim(rtrim(ret))
end function
 
print numname(0)
print numname(1.0)
print numname(-1.7)
print numname(910000)
print numname(987654)
print numname(100000017)
 