Re: Numbers to words

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I did some searching and found the function on the PHP web site.  Ron

<?
/**
* 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
?>

On Thu, 2006-02-09 at 22:18 -0500, Ron Piggott (PHP) wrote:
> Is there a command within PHP that would convert numbers to words:
> 
> echo "1";
> 
> # 1 appears on screen
> 
> echo command('1');
> 
> # one appears on screen --- what would the command be?
> 
> Ron

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux