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.


FUNCTION int2Text (number AS QUAD) AS STRING
    IF 0 = number THEN
        FUNCTION = "zero"
        EXIT FUNCTION
    END IF
 
    DIM num AS QUAD, outP AS STRING, unit AS LONG
    DIM tmpLng1 AS QUAD
 
    DIM small(1 TO 19) AS STRING, tens(7) AS STRING, big(5) AS STRING
 
    DIM tmpInt AS LONG, dcnt AS LONG
 
    ARRAY ASSIGN small() = "one", "two", "three", "four", "five", "six", _
                           "seven", "eight", "nine", "ten", "eleven", _
                           "twelve", "thirteen", "fourteen", "fifteen", _
                           "sixteen", "seventeen", "eighteen", "nineteen"
    ARRAY ASSIGN tens() = "twenty", "thirty", "forty", "fifty", "sixty", _
                          "seventy", "eighty", "ninety"
    ARRAY ASSIGN big() = "thousand", "million", "billion", "trillion", _
                         "quadrillion", "quintillion"
 
    num = ABS(number)
 
    DO
        tmpLng1 = num MOD 100
        SELECT CASE tmpLng1
            CASE 1 TO 19
                outP = small(tmpLng1) + " " + outP
            CASE 20 TO 99
                SELECT CASE tmpLng1 MOD 10
                    CASE 0
                        outP = tens((tmpLng1 \ 10) - 2) + " " + outP
                    CASE ELSE
                        outP = tens((tmpLng1 \ 10) - 2) + "-" + small(tmpLng1 MOD 10) + " " + outP
                END SELECT
        END SELECT
 
        tmpLng1 = (num MOD 1000) \ 100
        IF tmpLng1 THEN
            outP = small(tmpLng1) + " hundred " + outP
        END IF
 
        num = num \ 1000
        IF num < 1 THEN EXIT DO
 
        tmpLng1 = num MOD 1000
        IF tmpLng1 THEN outP = big(unit) + " " + outP
 
        unit = unit + 1
    LOOP
 
    IF number < 0 THEN outP = "negative " + outP
 
    FUNCTION = RTRIM$(outP)
END FUNCTION
 
 
FUNCTION PBMAIN () AS LONG
    DIM n AS QUAD
 
    #IF %DEF(%PB_CC32)
        INPUT "Gimme a number! ", n
    #ELSE
        n = VAL(INPUTBOX$("Gimme a number!", "Now!"))
    #ENDIF
    ? int2Text(n)
END FUNCTION