Numbers Converted Into Words

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

-242,832,452,151,924,352 = negative two hundred and forty-two quadrillion eight hundred and thirty-two trillion four hundred and fifty-two billion one hundred and fifty-one million nine hundred and twenty-four thousand three hundred and fifty-two
0 = zero
4 = four
99 = ninety-nine
903 = nine hundred and three
4,508 = four thousand five hundred and eight
92,207 = ninety-two thousand two hundred and seven
747,953 = seven hundred and forty-seven thousand nine hundred and fifty-three
9,668,742 = nine million six hundred and sixty-eight thousand seven hundred and forty-two
73,015,969 = seventy-three million fifteen thousand nine hundred and sixty-nine
848,870,384 = eight hundred and forty-eight million eight hundred and seventy thousand three hundred and eighty-four
4,107,090,243 = four billion one hundred and seven million ninety thousand two hundred and forty-three
36,677,965,042 = thirty-six billion six hundred and seventy-seven million nine hundred and sixty-five thousand forty-two
953,949,528,513 = nine hundred and fifty-three billion nine hundred and forty-nine million five hundred and twenty-eight thousand five hundred and thirteen
2,365,195,995,662 = two trillion three hundred and sixty-five billion one hundred and ninety-five million nine hundred and ninety-five thousand six hundred and sixty-two
89,174,058,334,901 = eighty-nine trillion one hundred and seventy-four billion fifty-eight million three hundred and thirty-four thousand nine hundred and one
669,211,150,147,020 = six hundred and sixty-nine trillion two hundred and eleven billion one hundred and fifty million one hundred and forty-seven thousand twenty
2,057,780,979,666,859 = two quadrillion fifty-seven trillion seven hundred and eighty billion nine hundred and seventy-nine million six hundred and sixty-six thousand eight hundred and fifty-nine
59,123,329,324,647,784 = fifty-nine quadrillion one hundred and twenty-three trillion three hundred and twenty-nine billion three hundred and twenty-four million six hundred and forty-seven thousand seven hundred and eighty-four
294,217,297,155,410,048 = two hundred and ninety-four quadrillion two hundred and seventeen trillion two hundred and ninety-seven billion one hundred and fifty-five million four hundred and ten thousand 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>