Numbers Converted Into Words

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

-833,187,256,054,952,704 = negative eight hundred and thirty-three quadrillion one hundred and eighty-seven trillion two hundred and fifty-six billion fifty-four million nine hundred and fifty-two thousand seven hundred and four
0 = zero
1 = one
28 = twenty-eight
664 = six hundred and sixty-four
8,879 = eight thousand eight hundred and seventy-nine
94,890 = ninety-four thousand eight hundred and ninety
463,942 = four hundred and sixty-three thousand nine hundred and forty-two
6,343,721 = six million three hundred and forty-three thousand seven hundred and twenty-one
42,957,291 = forty-two million nine hundred and fifty-seven thousand two hundred and ninety-one
193,827,069 = one hundred and ninety-three million eight hundred and twenty-seven thousand sixty-nine
8,024,648,161 = eight billion twenty-four million six hundred and forty-eight thousand one hundred and sixty-one
33,496,917,495 = thirty-three billion four hundred and ninety-six million nine hundred and seventeen thousand four hundred and ninety-five
949,057,308,351 = nine hundred and forty-nine billion fifty-seven million three hundred and eight thousand three hundred and fifty-one
6,785,992,576,275 = six trillion seven hundred and eighty-five billion nine hundred and ninety-two million five hundred and seventy-six thousand two hundred and seventy-five
52,444,656,873,121 = fifty-two trillion four hundred and forty-four billion six hundred and fifty-six million eight hundred and seventy-three thousand one hundred and twenty-one
895,627,051,405,608 = eight hundred and ninety-five trillion six hundred and twenty-seven billion fifty-one million four hundred and five thousand six hundred and eight
4,153,419,553,302,229 = four quadrillion one hundred and fifty-three trillion four hundred and nineteen billion five hundred and fifty-three million three hundred and two thousand two hundred and twenty-nine
93,795,387,623,831,632 = ninety-three quadrillion seven hundred and ninety-five trillion three hundred and eighty-seven billion six hundred and twenty-three million eight hundred and thirty-one thousand six hundred and thirty-two
584,552,922,286,093,312 = five hundred and eighty-four quadrillion five hundred and fifty-two trillion nine hundred and twenty-two billion two hundred and eighty-six million ninety-three thousand three hundred and twelve

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>