Numbers Converted Into Words

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

-362,777,098,035,439,872 = negative three hundred and sixty-two quadrillion seven hundred and seventy-seven trillion ninety-eight billion thirty-five million four hundred and thirty-nine thousand eight hundred and seventy-two
0 = zero
6 = six
64 = sixty-four
516 = five hundred and sixteen
5,207 = five thousand two hundred and seven
66,210 = sixty-six thousand two hundred and ten
877,312 = eight hundred and seventy-seven thousand three hundred and twelve
3,232,522 = three million two hundred and thirty-two thousand five hundred and twenty-two
42,789,950 = forty-two million seven hundred and eighty-nine thousand nine hundred and fifty
576,555,500 = five hundred and seventy-six million five hundred and fifty-five thousand five hundred
6,408,299,748 = six billion four hundred and eight million two hundred and ninety-nine thousand seven hundred and forty-eight
47,557,528,843 = forty-seven billion five hundred and fifty-seven million five hundred and twenty-eight thousand eight hundred and forty-three
873,473,240,854 = eight hundred and seventy-three billion four hundred and seventy-three million two hundred and forty thousand eight hundred and fifty-four
2,759,307,751,897 = two trillion seven hundred and fifty-nine billion three hundred and seven million seven hundred and fifty-one thousand eight hundred and ninety-seven
14,716,348,680,667 = fourteen trillion seven hundred and sixteen billion three hundred and forty-eight million six hundred and eighty thousand six hundred and sixty-seven
703,476,320,020,854 = seven hundred and three trillion four hundred and seventy-six billion three hundred and twenty million twenty thousand eight hundred and fifty-four
3,630,968,898,534,775 = three quadrillion six hundred and thirty trillion nine hundred and sixty-eight billion eight hundred and ninety-eight million five hundred and thirty-four thousand seven hundred and seventy-five
22,442,477,648,146,448 = twenty-two quadrillion four hundred and forty-two trillion four hundred and seventy-seven billion six hundred and forty-eight million one hundred and forty-six thousand four hundred and forty-eight
495,480,342,907,831,104 = four hundred and ninety-five quadrillion four hundred and eighty trillion three hundred and forty-two billion nine hundred and seven million eight hundred and thirty-one thousand one hundred and four

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>