Re: is there a number translation function?
| From: | tg-php at gryffyndevelopment dot com | Date: | Sat, 29 Oct 2005 18:19:47 +0000 |
| Subject: | Re: is there a number translation function? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-224816@lists.php.net to get a copy of this message | ||
I couldn't find one out there Linda (and I'm sure I've seen them before) so since
I'm stuck at work on a Saturday I thought I'd take a break and see if I could hack this
out myself.
Probably a more elegant way to do this, but here's what I came up with. Feel free to use and
modify it at will.
It should be infinitely expandable by adding to the "$places" array. (well,
'infinite' in regards to your system's ability to handle long strings, no long ints
or floats are used so that shouldn't limit it).
Let me know if you have any questions or if anyone can improve this (always up for learning new
tricks :)
Good luck Linda!
-TG
<?PHP
$numerals = array(1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "fourteen",
15 => "fifteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen",
20 => "twenty",
30 => "thirty",
40 => "fourty",
50 => "fifty",
60 => "sixty",
70 => "seventy",
80 => "eighty",
90 => "ninety"
);
$places = array(1 => "hundred",
2 => "thousand",
3 => "million",
4 => "billion",
5 => "trillion",
6 => "quadrillion"
);
$testnumber = "1,205,513";
$number = str_replace(",", "", trim($testnumber));
$rev_number = strrev($number);
$rev_numberarr = explode("|||", wordwrap($rev_number, 3, "|||", 1));
$x = 1;
$outarr = array();
foreach ($rev_numberarr as $order => $rev_num_block) {
$tmpoutput = "";
$num_block = sprintf("%03s", strrev($rev_num_block));
list($pos_one, $pos_two, $pos_three) = explode("|||", wordwrap($num_block, 1,
"|||", 1));
if (intval($pos_one) <> 0) $tmpoutput = $numerals[$pos_one] . " hundred " .
$tmpoutput . " ";
if (intval($pos_two . $pos_three) <= 20 OR (intval($pos_two) <> 0 AND intval($pos_three)
== 0)) {
$tmpoutput .= $numerals[intval($pos_two . $pos_three)];
} elseif (intval($pos_two) == 0) {
$tmpoutput .= $numerals[$pos_three] . $tmpoutput;
} elseif (intval($pos_two) <> 0 AND intval($pos_three) <> 0) {
$tmpoutput .= $numerals[$pos_two . $pos_three] . $tmpoutput;
}
if ($x > 1) $tmpoutput .= " " . $places[$x];
array_unshift($outarr, $tmpoutput);
//$output = $tmpoutput . " " . $output; # Can just use this instead of
"array_unshift" if you don't need to put comma dividers
$x++;
}
$output = implode(", ", $outarr); // If you don't use an array, remove this line too
echo $testnumber . "<br>\n";
echo "becomes<br>\n";
echo "\"" . ucwords($output) . "\"";
?>
= = = Original message = = =
Hi,
Does anyone know of a function for translating a decimal number into an
English number. In other words, if you pass it 1 it will return 'one', if
you pass it 127 it will return 'one hundred twenty seven', and etc.
Thanks,
Linda
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.