note 40136 deleted from function.number-format by levim

From: Date: Sun, 23 Mar 2014 14:16:53 +0000
Subject: note 40136 deleted from function.number-format by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199031@lists.php.net to get a copy of this message
Note Submitter: armstrong ~~at~~ rice ~~dot~~ edu ---- I submitted this question earlier, but I found the answer myself. To convert a number to its word form (e.g. 34 to "thirty four") try the function below. It turned out to be a lot more complex than I thought! I'm using it for printing dollar ammounts, so the cents get printed like 13/100 I converted most of it from this java code http://mindprod.com/inwords.html so credit goes to him for doing the hard part. <? /** * convert long integer into American English words. * e.g. -12345 -> "minus twelve thousand forty-five" * Handles negative and positive integers * on range -Long.MAX_VALUE .. Long.MAX_VALUE; * It cannot handle Long.MIN_VALUE; */ function num2words( $num ){ $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 = $inWords($remdr); } $s = $t . " " . $groupName[$i] . " " . $s; $num = (int)$num; } // end for $s = trim($s); if ( $negative ){ $s = $MINUS . " " . $s; } $s .= " and $cents/100"; return $s; } // end inWords ?>

« previous php.notes (#199031) next »