Numbers Converted Into Words

Here are some RINO (Random In Name Only) numbers:

-689,036,309,160,292,096 = negative six hundred and eighty-nine quadrillion thirty-six trillion three hundred and nine billion one hundred and sixty million two hundred and ninety-two thousand ninety-six
0 = zero
3 = three
66 = sixty-six
523 = five hundred and twenty-three
1,325 = one thousand three hundred and twenty-five
98,096 = ninety-eight thousand ninety-six
421,285 = four hundred and twenty-one thousand two hundred and eighty-five
3,676,748 = three million six hundred and seventy-six thousand seven hundred and forty-eight
68,432,183 = sixty-eight million four hundred and thirty-two thousand one hundred and eighty-three
342,956,073 = three hundred and forty-two million nine hundred and fifty-six thousand seventy-three
5,252,724,414 = five billion two hundred and fifty-two million seven hundred and twenty-four thousand four hundred and fourteen
69,225,284,773 = sixty-nine billion two hundred and twenty-five million two hundred and eighty-four thousand seven hundred and seventy-three
759,774,587,629 = seven hundred and fifty-nine billion seven hundred and seventy-four million five hundred and eighty-seven thousand six hundred and twenty-nine
8,359,310,938,511 = eight trillion three hundred and fifty-nine billion three hundred and ten million nine hundred and thirty-eight thousand five hundred and eleven
92,329,525,067,470 = ninety-two trillion three hundred and twenty-nine billion five hundred and twenty-five million sixty-seven thousand four hundred and seventy
438,751,762,639,731 = four hundred and thirty-eight trillion seven hundred and fifty-one billion seven hundred and sixty-two million six hundred and thirty-nine thousand seven hundred and thirty-one
5,068,193,136,248,738 = five quadrillion sixty-eight trillion one hundred and ninety-three billion one hundred and thirty-six million two hundred and forty-eight thousand seven hundred and thirty-eight
14,709,151,894,785,464 = fourteen quadrillion seven hundred and nine trillion one hundred and fifty-one billion eight hundred and ninety-four million seven hundred and eighty-five thousand four hundred and sixty-four
611,524,331,429,973,248 = six hundred and eleven quadrillion five hundred and twenty-four trillion three hundred and thirty-one billion four hundred and twenty-nine million nine hundred and seventy-three thousand two hundred and forty-eight

The output above was produced with:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">        
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <title>Translate Digits to Words</title> 
    </head> 
    <body> 
<h1>Numbers Converted Into Words</h1>
<p>Here are some RINO (Random In Name Only) numbers:</p>
<?php
    
// Example usage:
    // http://en.wikipedia.org/wiki/Names_of_large_numbers
    
echo "<!-- Tables are for tabular data -->";
    echo 
"<table>";
    
$maximum_size pow(10,18);
    
$number mt_rand($maximum_size/10$maximum_size 1);
    
// careful with the formatting of large numbers, errors are introduced in the result if default formatting is preserved
    
echo "<tr><td>-" number_format($number0"."",") . "</td><td> = <strong>" translateToWords(intval(number_format(-1*$number0""""))) . "</strong></td></tr>";     
    echo 
"<tr><td>" number_format(00"."",") . "</td><td> = <strong>" translateToWords(0) . "</strong></td></tr>"
    for (
$max pow(10,1); $max <= $maximum_size$max *= pow(10,1)) 
    {
        
$number mt_rand($max 10$max 1);
        echo 
"<tr><td>" number_format($number0"."",") . "</td><td> = <strong>" translateToWords(intval(number_format($number0""""))) . "</strong></td></tr>"
    }        
    echo 
"</table>";
    function 
translateToWords($number
    {
    
/*****
         * A recursive function to turn digits into words
         * Numbers must be integers from -999,999,999,999 to 999,999,999,999 inclussive.    
         *
         *  (C) 2010 Peter Ajtai
         *    This program is free software: you can redistribute it and/or modify
         *    it under the terms of the GNU General Public License as published by
         *    the Free Software Foundation, either version 3 of the License, or
         *    (at your option) any later version.
         *
         *    This program is distributed in the hope that it will be useful,
         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
         *    GNU General Public License for more details.
         *
         *    See the GNU General Public License: <http://www.gnu.org/licenses/>.
         *
         */
        // zero is a special case, it cause problems even with typecasting if we don't deal with it here
        
$max_size pow(10,18);
        if (!
$number) return "zero";
        if (
is_int($number) && $number abs($max_size)) 
        {            
            switch (
$number
            {
                
// set up some rules for converting digits to words
                
case $number 0:
                    
$prefix "negative";
                    
$suffix translateToWords(-1*$number);
                    
$string $prefix " " $suffix;
                    break;
                case 
1:
                    
$string "one";
                    break;
                case 
2:
                    
$string "two";
                    break;
                case 
3:
                    
$string "three";
                    break;
                case 
4
                    
$string "four";
                    break;
                case 
5:
                    
$string "five";
                    break;
                case 
6:
                    
$string "six";
                    break;
                case 
7:
                    
$string "seven";
                    break;
                case 
8:
                    
$string "eight";
                    break;
                case 
9:
                    
$string "nine";
                    break;                
                case 
10:
                    
$string "ten";
                    break;            
                case 
11:
                    
$string "eleven";
                    break;            
                case 
12:
                    
$string "twelve";
                    break;            
                case 
13:
                    
$string "thirteen";
                    break;            
                
// fourteen handled later
                
case 15:
                    
$string "fifteen";
                    break;            
                case 
$number 20:
                    
$string translateToWords($number%10);
                    
// eighteen only has one "t"
                    
if ($number == 18)
                    {
                    
$suffix "een";
                    } else 
                    {
                    
$suffix "teen";
                    }
                    
$string .= $suffix;
                    break;            
                case 
20:
                    
$string "twenty";
                    break;            
                case 
30:
                    
$string "thirty";
                    break;            
                case 
40:
                    
$string "forty";
                    break;            
                case 
50:
                    
$string "fifty";
                    break;            
                case 
60:
                    
$string "sixty";
                    break;            
                case 
70:
                    
$string "seventy";
                    break;            
                case 
80:
                    
$string "eighty";
                    break;            
                case 
90:
                    
$string "ninety";
                    break;                
                case 
$number 100:
                    
$prefix translateToWords($number-$number%10);
                    
$suffix translateToWords($number%10);
                    
$string $prefix "-" $suffix;
                    break;
                
// handles all number 100 to 999
                
case $number pow(10,3):                    
                    
// floor return a float not an integer
                    
$prefix translateToWords(intval(floor($number/pow(10,2)))) . " hundred";
                    if (
$number%pow(10,2)) $suffix " and " translateToWords($number%pow(10,2));
                    
$string $prefix $suffix;
                    break;
                case 
$number pow(10,6):
                    
// floor return a float not an integer
                    
$prefix translateToWords(intval(floor($number/pow(10,3)))) . " thousand";
                    if (
$number%pow(10,3)) $suffix translateToWords($number%pow(10,3));
                    
$string $prefix " " $suffix;
                    break;
                case 
$number pow(10,9):
                    
// floor return a float not an integer
                    
$prefix translateToWords(intval(floor($number/pow(10,6)))) . " million";
                    if (
$number%pow(10,6)) $suffix translateToWords($number%pow(10,6));
                    
$string $prefix " " $suffix;
                    break;                    
                case 
$number pow(10,12):
                    
// floor return a float not an integer
                    
$prefix translateToWords(intval(floor($number/pow(10,9)))) . " billion";
                    if (
$number%pow(10,9)) $suffix translateToWords($number%pow(10,9));
                    
$string $prefix " " $suffix;    
                    break;
                case 
$number pow(10,15):
                    
// floor return a float not an integer
                    
$prefix translateToWords(intval(floor($number/pow(10,12)))) . " trillion";
                    if (
$number%pow(10,12)) $suffix translateToWords($number%pow(10,12));
                    
$string $prefix " " $suffix;    
                    break;        
                
// Be careful not to pass default formatted numbers in the quadrillions+ into this function
                // Default formatting is float and causes errors
                
case $number pow(10,18):
                    
// floor return a float not an integer
                    
$prefix translateToWords(intval(floor($number/pow(10,15)))) . " quadrillion";
                    if (
$number%pow(10,15)) $suffix translateToWords($number%pow(10,15));
                    
$string $prefix " " $suffix;    
                    break;                    
            }
        } else
        {
            echo 
"ERROR with - $number<br/> Number must be an integer between -" number_format($max_size0"."",") . " and " number_format($max_size0"."",") . " exclussive.";
        }
        return 
$string;    
    }
?>
<p>The output above was produced with:</p>
<?php
highlight_file
(__FILE__);
?>
    </body>
</html>