Re: int to string

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

 



Hi Tanner,

Here's a completely working piece of code that I got from my hosting company. This looks like it does exactly what you want to do. I am also including the output in the bottom of this e-mail.

sunil.
<?
 /******************************************************************
 **
 ** Class: num2str
 ** Description: This class converts a number into its corresponding
 **    text represenation.
 **    e.g., input: 1,001 => output: one thousand one
 ** Written By: Websulting (www dot websulting dot com)
 ** Copyright: You are free to use this script as you like.  This
 **    script come with absolutely no guarantee.  You must leave
 **    this copyright notice with this file.
 **    Report bugs to: contact at websulting dot com
 **
 ******************************************************************/
 class num2str {
   var $one_a=array(
       "one","two","three","four","five","six","seven",
       "eight","nine","ten","eleven","twelve","thirteen",
       "fourteen","fifteen","sixteen","seventeen","eighteen","ninteen");
   var $ten_a=array(
       "ten","twenty","thirty","forty","fifty","sixty","seventy",
       "eighty","ninty");
   var $mil_a=array("thousand","million","billion","trillion");

   //*********************************************************
   //
   // Method: convert
   // Description: Convert the input number to string representation
   //
   //*********************************************************
   function convert ($num) {
     $triplets = preg_split('/,/',$num,-1,PREG_SPLIT_NO_EMPTY);
     $length = count($triplets);
     $plc_a= array();
     for ($i=0;$i<$length;$i++) {$plc_a[$i]=$this->tonum($triplets[$i]);}
     $rval = "";
     foreach ($plc_a as $k) {
       if ($k == "") {$length--;continue;}
       $rval .= "$k ". $this->getplace($length--). " ";
     }
     return $rval;
   }

   //*********************************************************
   //
   // Method: getplace
   // Description: Get the place of the digits.
   //
   //*********************************************************
   function getplace($i) {
     return $this->mil_a[$i-2];
   }

   //*********************************************************
   //
   // Method: tonum
   // Description: Convert the individual set to text
   //
   //*********************************************************
   function tonum ($num) {
     $len = strlen($num);
     switch ($len) {
       case 3:
         if (substr($num,1,2) <=19) {
           return $this->one_a[substr($num,0,1)-1].
             (substr($num,0,1)==0 ? "" : " hundred ").
             $this->one_a[substr($num,1,2)-1];
         }
         return $this->one_a[substr($num,0,1)-1] .
             (substr($num,0,1)==0 ? "" : " hundred ").
             $this->ten_a[substr($num,1,1)-1]." ".
             $this->one_a[substr($num,2,1)-1];
       case 2:
         if (substr($num,0,2) <=19) {
           return $this->one_a[substr($num,0,2)-1];
         }
         return $this->ten_a[substr($num,0,1)-1]." ".
         $this->one_a[substr($num,1,1)-1];
       case 1:
         return $this->one_a[substr($num,0,1)-1];
     }
   }
 }
 //End of class num2str

 $obj = new num2str();
foreach (array("1,121","121","1,000,010,000,321","1,109,019,000,321", "100","70","10","15","5") as $num) {
   print "[$num] => " . $obj->convert($num)."\n";
 }
?>

output:

[1,121] => one thousand one hundred twenty one
[121] => one hundred twenty one
[1,000,010,000,321] => one trillion ten million three hundred twenty one
[1,109,019,000,321] => one trillion one hundred nine billion ninteen million three hundred twenty one
[100] => one hundred
[70] => seventy
[10] => ten
[15] => fifteen
[5] => five

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux