Numbers Converted Into Words

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

-816,559,562,599,286,400 = negative eight hundred and sixteen quadrillion five hundred and fifty-nine trillion five hundred and sixty-two billion five hundred and ninety-nine million two hundred and eighty-six thousand four hundred
0 = zero
4 = four
67 = sixty-seven
801 = eight hundred and one
8,107 = eight thousand one hundred and seven
36,484 = thirty-six thousand four hundred and eighty-four
237,493 = two hundred and thirty-seven thousand four hundred and ninety-three
2,260,043 = two million two hundred and sixty thousand forty-three
60,305,871 = sixty million three hundred and five thousand eight hundred and seventy-one
136,442,788 = one hundred and thirty-six million four hundred and forty-two thousand seven hundred and eighty-eight
8,056,926,024 = eight billion fifty-six million nine hundred and twenty-six thousand twenty-four
15,449,255,923 = fifteen billion four hundred and forty-nine million two hundred and fifty-five thousand nine hundred and twenty-three
457,952,039,875 = four hundred and fifty-seven billion nine hundred and fifty-two million thirty-nine thousand eight hundred and seventy-five
6,890,910,132,322 = six trillion eight hundred and ninety billion nine hundred and ten million one hundred and thirty-two thousand three hundred and twenty-two
24,143,002,522,177 = twenty-four trillion one hundred and forty-three billion two million five hundred and twenty-two thousand one hundred and seventy-seven
122,036,569,099,873 = one hundred and twenty-two trillion thirty-six billion five hundred and sixty-nine million ninety-nine thousand eight hundred and seventy-three
7,455,213,758,628,816 = seven quadrillion four hundred and fifty-five trillion two hundred and thirteen billion seven hundred and fifty-eight million six hundred and twenty-eight thousand eight hundred and sixteen
23,786,517,037,078,736 = twenty-three quadrillion seven hundred and eighty-six trillion five hundred and seventeen billion thirty-seven million seventy-eight thousand seven hundred and thirty-six
240,813,172,049,820,416 = two hundred and forty quadrillion eight hundred and thirteen trillion one hundred and seventy-two billion forty-nine million eight hundred and twenty thousand four hundred and sixteen

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>