PHP – Class Convert Numeric Value into String Value

The given PHP class helps you to convert the numeric value into the respective string value.

Code Example: Download Example

index.php file
<?php
include_once("words_class.php");
$re = new in_words();
$num1 = "585677524598098454.079";
$num2 = 4232878.98;
$num3 = 468;
$num4 = 12.765;
$num5 = 0.67;
echo "$num1 =>" . $re->show_words($num1) .
 "<br /> $num2 => ". $re->show_words($num2) .
 "<br /> $num3 => ". $re->show_words($num3) .  
 "<br /> $num4 => ". $re->show_words($num4) .  
 "<br /> $num5 => ". $re->show_words($num5);
?>
words_class.php file
<?php
class in_words{

 var $word = "";
 var $decimal;
 var $decimal_text = '';

 function in_words(){                       
 $this->aa = array(" ", "Hundred", "Thousand", "Million", "Billion", "Trillion", "Quadrillion");  
 $this->tens = array(" ", " ", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty",
                     "Ninty");
 $this->on = array("Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", 
                   "Seventeen", "Eighteen", "Ninteen");
 $this->ones = array(" ", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine");
 }

 function show_words($fig){
 $this->fig = ereg_replace(",", "", $fig);              // REMEOVE ANY , FROM THE NUMBER
 $this->spilt_decimals();                               // CHECK FOR THE DECIMAL PART

 $this->rr = explode(",", number_format($this->fig));   // SEPARATE THE 3 DIGITS INTO ARRAY ELEMENTS       
 $this->mx = count($this->rr);
 $this->fig = strtolower($this->fig);  
 $this->compose();
 $this->handle_decimal();
 return $this->word.($this->decimal_text ? " <b>point</b> ".$this->decimal_text : '');
 }

 function spilt_decimals(){
 $n = explode(".", $this->fig);
 $this->fig = $n[0];
 $this->decimal = $n[1];
 }

 function compose(){
 $this->word = ''; // TO RESET THE VALUE FOR MULTIPLE INSTANCES
 if($this->mx == 1 && $this->rr[0] == 0) $this->word = "Zero";
 else if($this->mx > 6) $this->word = "Figure NOT available in words"; # If quadrillion range out, IGNORE
 else{
 for($i=0; $i<$this->mx; $i++){
 $k = $this->mx - $i;
 $this->word .= $this->handle_3($this->rr[$i])." ".
               ($k > 1 ? ($this->rr[$i] == '000' ? '' : $this->aa[$k]). " " : '');       
 }
 }
 }

 function handle_3($num){

 $num = $num[2].$num[1].$num[0];
 if($num[1] == '1') $text = ($num[2] ? $this->ones[$num[2]]." Hundred ": "").
                            ($num[1] ? $this->on[$num[0]]." " : ""); 

 else $text = ($num[2] ? $this->ones[$num[2]]." Hundred ": "").
              ($num[1] ? $this->tens[$num[1]]." " : "").$this->ones[$num[0]]; 

 return $text;
 }

 function handle_decimal(){
 $this->decimal_text = "";
 for($i=0; $i< strlen($this->decimal); $i++){
 if($this->decimal[$i] == 0) $this->decimal_text .= " Zero ";
 else $this->decimal_text .= " ".$this->ones[$this->decimal[$i]];
 }

 }

}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

2 Comments

  1. avatarGeeth

    Many thanks… It’s working Grate…
    I have small thing..

    Eg: 4350.50 (Four Thousand Three Hundred Fifty point Five)<– display

    But it should be (Four Thousand Three Hundred Fifty point Fifty Cents)

    How can we make this…

    thanks
    geeth

  2. avatarChirs

    You may explode the 4350.55 value with point(.) and then pass it to the show_words function.

    Eg:
    $num = 4350.55;
    $parts = explode(“.”,$num);

    echo “$num1 =>” . $re->show_words($parts[0]).’ Points ‘.$re->show_words($parts[1]) . “cents”;

Leave a Comment

Your email address will not be published. Required fields are marked *