note 66895 added to function.number-format
| From: | shpoffo dot a dot t dot dot gmail dot dot dot at osu1 dot php dot net | Date: | Wed, 31 May 2006 16:31:39 +0000 |
| Subject: | note 66895 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-112658@lists.php.net to get a copy of this message | ||
For those that need to print the word-form of a number ("twenty-three" instead of
"23") I've found the following code to be useful. This code is an improvement upon
that posted in 2004 by armstrong @ rice. Notably, the previous code did not properly compute large
numbers (at least in PHP 4.x that I tested).
The following code adds another variable to the function, which controls the display of fractional
parts, and properly calculates values one thousand and above.
Zak
~ ~ ~
function num2words($num, $c=1) {
$ZERO = 'zero';
$MINUS = 'minus';
$lowName = array(
/* zero is shown as "" since it is never used in combined forms */
/* 0 .. 19 */
"", "one", "two", "three", "four",
"five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen",
"fifteen",
"sixteen", "seventeen", "eighteen", "nineteen");
$tys = array(
/* 0, 10, 20, 30 ... 90 */
"", "", "twenty", "thirty", "forty",
"fifty",
"sixty", "seventy", "eighty", "ninety");
$groupName = array(
/* We only need up to a quintillion, since a long is about 9 * 10 ^ 18 */
/* American: unit, hundred, thousand, million, billion, trillion, quadrillion, quintillion */
"", "hundred", "thousand", "million",
"billion",
"trillion", "quadrillion", "quintillion");
$divisor = array(
/* How many of this group is needed to form one of the succeeding group. */
/* American: unit, hundred, thousand, million, billion, trillion, quadrillion, quintillion */
100, 10, 1000, 1000, 1000, 1000, 1000, 1000) ;
$num = str_replace(",","",$num);
$num = number_format($num,2,'.','');
$cents = substr($num,strlen($num)-2,strlen($num)-1);
$num = (int)$num;
$s = "";
if ( $num == 0 ) $s = $ZERO;
$negative = ($num < 0 );
if ( $negative ) $num = -$num;
// Work least significant digit to most, right to left.
// until high order part is all 0s.
for ( $i=0; $num>0; $i++ ) {
$remdr = (int)($num % $divisor[$i]);
$num = $num / $divisor[$i];
// check for 1100 .. 1999, 2100..2999, ... 5200..5999
// but not 1000..1099, 2000..2099, ...
// Special case written as fifty-nine hundred.
// e.g. thousands digit is 1..5 and hundreds digit is 1..9
// Only when no further higher order.
if ( $i == 1 /* doing hundreds */ && 1 <= $num && $num <= 5 ){
if ( $remdr > 0 ){
$remdr = ($num * 10);
$num = 0;
} // end if
} // end if
if ( $remdr == 0 ){
continue;
}
$t = "";
if ( $remdr < 20 ){
$t = $lowName[$remdr];
}
else if ( $remdr < 100 ){
$units = (int)$remdr % 10;
$tens = (int)$remdr / 10;
$t = $tys [$tens];
if ( $units != 0 ){
$t .= "-" . $lowName[$units];
}
}else {
$t = num2words($remdr, 0);
}
$s = $t." ".$groupName[$i]." ".$s;
$num = (int)$num;
} // end for
$s = trim($s);
if ( $negative ){
$s = $MINUS . " " . $s;
}
if ($c == 1) $s .= " and $cents/100";
return $s;
} // end num2words
----
Server IP: 64.71.164.2
Probable Submitter: 67.40.225.72
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.number-format.php
Edit -- http://master.php.net/note/edit/66895
Del: integrated -- http://master.php.net/note/delete/66895/integrated
Del: useless -- http://master.php.net/note/delete/66895/useless
Del: bad code -- http://master.php.net/note/delete/66895/bad+code
Del: spam -- http://master.php.net/note/delete/66895/spam
Del: non-english -- http://master.php.net/note/delete/66895/non-english
Del: in docs -- http://master.php.net/note/delete/66895/in+docs
Del: other reasons-- http://master.php.net/note/delete/66895
Reject -- http://master.php.net/note/reject/66895
Search -- http://master.php.net/manage/user-notes.php