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.


SUBROUTINE NumberToWords(number)
 CHARACTER outP*255, small*130, tens*80, big*80
 REAL ::   decimal_places = 7
 INIT( APPENDIX("#literals"), small, tens, big)
 
 num = ABS( INT(number) )
 order = 0
 outP = ' '
 DO i = 1, num + 1
   tmp = MOD(num, 100)
   IF(tmp > 19) THEN
       EDIT(Text=tens, ITeM=INT(MOD(tmp/10, 10)), Parse=medium)
       IF( MOD(tmp, 10) ) THEN
           EDIT(Text=small, ITeM=MOD(tmp,10)+1, Parse=mini)
           outP = medium // '-' // mini // ' ' // outP
       ELSE
           outP = medium // ' ' // outP
       ENDIF
   ELSEIF(tmp > 0) THEN
       EDIT(Text=small, ITeM=tmp+1, Parse=mini)
       outP = mini // ' '// outP
   ELSEIF(number == 0) THEN
       outP = 'zero'
   ENDIF
 
   tmp = INT(MOD(num, 1000) / 100)
   IF(tmp) THEN
       EDIT(Text=small, ITeM=tmp+1, Parse=oneto19)
       outP = oneto19 // ' hundred ' // outP
   ENDIF
 
   num = INT(num /1000)
   IF( num == 0) THEN
       IF(number < 0) outP = 'minus ' // outP
       fraction = ABS( MOD(number, 1) )
       IF(fraction) WRITE(Text=outP, APPend) ' point'
       DO j = 1, decimal_places
         IF( fraction >= 10^(-decimal_places) ) THEN
             num = INT( 10.01 * fraction )
             EDIT(Text=small, ITeM=num+1, Parse=digit)
             WRITE(Text=outP, APPend) ' ', digit
             fraction = 10*fraction - num
         ENDIF
       ENDDO
       OPEN(FIle="temp.txt", APPend)
       WRITE(FIle="temp.txt", Format='F10, " = ", A', CLoSe=1) number, outP
       RETURN
   ENDIF
 
   order = order + 1
   EDIT(Text=big, ITeM=order, Parse=kilo)
   IF( MOD(num, 1000) ) outP = kilo // ' and '// outP
 ENDDO
END
 
CALL NumberToWords( 0 )
CALL NumberToWords( 1234 )
CALL NumberToWords( 1234/100 )
CALL NumberToWords( 10000000 + 1.2 )
CALL NumberToWords( 2^15 )
CALL NumberToWords( 0.001 )
CALL NumberToWords( -EXP(1) )
 
#literals
 SMALL= zero one two three four five six seven eight nine ten &
 eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
 
 TENS=ten twenty thirty forty fifty sixty seventy eighty ninety
 
 BIG=thousand million billion trillion quadrillion