note 27394 added to function.number-format
| From: | php at iamcal dot com | Date: | Wed, 04 Dec 2002 12:31:39 +0000 |
| Subject: | note 27394 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40338@lists.php.net to get a copy of this message | ||
here's a more compact and speedy version:
function readable_number($a){
$bits_a = array("thousand", "million", "billion",
"trillion", "zillion");
$bits_b = array("ten", "twenty", "thirty", "fourty",
"fifty", "sixty", "seventy", "eighty", "ninety");
$bits_c = array("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen",
"nineteen");
if ($a==0){return 'zero';}
$out = ($a<0)?'minus ':'';
$a = abs($a);
for($i=count($bits_a); $i>0; $i--){
$p = pow(1000, $i);
if ($a > $p){
$b = floor($a/$p);
$a -= $p * $b;
$out .= readable_number($b).' '.$bits_a[$i-1].(($a)?', ':'');
}
}
if ($a > 100){
$b = floor($a/100);
$a -= 100 * $b;
$out .= readable_number($b).' hundred'.(($a)?' and ':' ');
}
if ($a >= 20){
$b = floor($a/10);
$a -= 10 * $b;
$out .= $bits_b[$b-1].' ';
}
if ($a){
$out .= $bits_c[$a-1];
}
return $out;
}
some sample output:
8 = eight
12 = twelve
-27 = minus twenty seven
186 = one hundred and eighty six
1,234,567 = one million, two hundred and thirty four thousand, five hundred and sixty seven
--
http://www.php.net/manual/en/function.number-format.php
http://master.php.net/manage/user-notes.php?action=edit+27394
http://master.php.net/manage/user-notes.php?action=delete+27394
http://master.php.net/manage/user-notes.php?action=reject+27394