The code below is sometimes wrong when reading the decimal value if the number exceeded e.g: 280,999,999,999,999.99 it will be read with: Two Hundred and Eighty-One Trillion, and also if the number is like: 99,999,999,999,999.99 its decimal will be read: Ninety-Eight, so I've stuck with it, anyone could help me please? Thanks in advance class number_word { private $word_array = array(1=>"One",2=>"Two",3=>"Three",4=>"Four",5=>"Five",6=>"Six",7=>"Seven",8=>"Eight",9=>"Nine",10=>"Ten",11=>"Eleven",12=>"Twelve",13=>"Thirteen",14=>"Fourteen",15=>"Fifteen",16=>"Sixteen",17=>"Seventeen",18=>"Eighteen",19=>"Nineteen",20=>"Twenty",21=>"Twenty-One",22=>"Twenty-Two",23=>"Twenty-Three",24=>"Twenty-Four",25=>"Twenty-Five",26=>"Twenty-Six",27=>"Twenty-Seven",28=>"Twenty-Eight",29=>"Twenty-Nine",30=>"Thirty",31=>"Thirty-One",32=>"Thirty-Two",33=>"Thirty-Three",34=>"Thirty-Four",35=>"Thirty-Five",36=>"Thirty-Six",37=>"Thirty-Seven",38=>"Thirty-Eight",39=>"Thirty-Nine",40=>"Forty",41=>"Forty-One",42=>"Forty-Two",43=>"Forty-Three",44=>"Forty-Four",45=>"Forty-Five",46=>"Forty-Six",47=>"Forty-Seven",48=>"Forty-Eight",49=>"Forty-Nine",50=>"Fifty",51=>"Fifty-One",52=>"Fifty-Two",53=>"Fifty-Three",54=>"Fifty-Four",55=>"Fifty-Five",56=>"Fifty-Six",57=>"Fifty-Seven",58=>"Fifty-Eight",59=>"Fifty-Nine",60=>"Sixty",61=>"Sixty-One",62=>"Sixty-Two",63=>"Sixty-Three",64=>"Sixty-Four",65=>"Sixty-Five",66=>"Sixty-Six",67=>"Sixty-Seven",68=>"Sixty-Eight",69=>"Sixty-Nine",70=>"Seventy",71=>"Seventy-One",72=>"Seventy-Two",73=>"Seventy-Three",74=>"Seventy-Four",75=>"Seventy-Five",76=>"Seventy-Six",77=>"Seventy-Seven",78=>"Seventy-Eight",79=>"Seventy-Nine",80=>"Eighty",81=>"Eighty-One",82=>"Eighty-Two",83=>"Eighty-Three",84=>"Eighty-Four",85=>"Eighty-Five",86=>"Eighty-Six",87=>"Eighty-Seven",88=>"Eighty-Eight",89=>"Eighty-Nine",90=>"Ninety",91=>"Ninety-One",92=>"Ninety-Two",93=>"Ninety-Three",94=>"Ninety-Four",95=>"Ninety-Five",96=>"Ninety-Six",97=>"Ninety-Seven",98=>"Ninety-Eight",99=>"Ninety-Nine",100=>"One Hundred"); private $thousand = array("", "Thousand, ", "Million, ", "Billion, ", "Trillion, ", "Zillion, "); private $val, $currency0, $currency1; private $val_array, $dec_value, $dec_word, $num_value, $num_word; var $val_word; public function number_word($in_val = 0, $in_currency0 = "", $in_currency1 = "") { $this->val = $in_val; $this->currency0 = $in_currency0; $this->currency1 = $in_currency1; $this->val = abs(floatval(str_replace(",","",$this->val))); if ($this->val > 0) { $this->val = number_format($this->val, '2', ',', ','); $this->val_array = explode(",", $this->val); $this->dec_value = intval($this->val_array[count($this->val_array) - 1]); if ($this->dec_value > 0) { $this->dec_word = $this->word_array[$this->dec_value]." ".$this->currency1; } $t = 0; $this->num_word = ""; for ($i = count($this->val_array) - 2; $i >= 0; $i--) { $this->num_value = intval($this->val_array[$i]); if ($this->num_value == 0) { $this->num_word = "".$this->num_word; } elseif (strlen($this->num_value."") <= 2) { $this->num_word = $this->word_array[$this->num_value]." ".$this->thousand[$t].$this->num_word; if ($i == 1) { $this->num_word = " and ".$this->num_word; } } else { $this->num_word = $this->word_array[substr($this->num_value, 0, 1)."00"]. (intval(substr($this->num_value, 1, 2)) > 0 ? " and " : "") .$this->word_array[intval(substr($this->num_value, 1, 2))]." ".$this->thousand[$t].$this->num_word; } $t++; } if (!empty($this->num_word)) { $this->num_word .= " ".$this->currency0; } } $this->val_word = $this->num_word." ".$this->dec_word; } } PHP:
You are experiencing overflow: http://www.herongyang.com/PHP/Data-Type-Overflow-of-Integer-and-Float-Values.html
$this->num_word = ""; for ($i = count($this->val_array) - 2; $i >= 0; $i--) { if(is_infinite(intval($this->val_array[$i]))){ // Number is too big insert some kind of error handling here }else{ $this->num_value = intval($this->val_array[$i]); if ($this->num_value == 0) { $this->num_word = "".$this->num_word; } } elseif (strlen($this->num_value."") <= 2) { $this->num_word = $this->word_array[$this->num_value]." ".$this->thousand[$t].$this->num_word; if ($i == 1) { $this->num_word = " and ".$this->num_word; } } Code (markup):
Hi, by following to the code if close } PHP: place bofore elseif (strlen($this->num_value."") <= 2) { PHP: then will be syntax error, unexpected T_ELSEIF, perhaps I missing something? Thanks
Try this: <?php class number_word { private $word_array = array(1=>"One",2=>"Two",3=>"Three",4=>"Four",5=>"Five",6=>"Six",7=>"Seven",8=>"Eight",9=>"Nine",10=>"Ten",11=>"Eleven",12=>"Twelve",13=>"Thirteen",14=>"Fourteen",15=>"Fifteen",16=>"Sixteen",17=>"Seventeen",18=>"Eighteen",19=>"Nineteen",20=>"Twenty",21=>"Twenty-One",22=>"Twenty-Two",23=>"Twenty-Three",24=>"Twenty-Four",25=>"Twenty-Five",26=>"Twenty-Six",27=>"Twenty-Seven",28=>"Twenty-Eight",29=>"Twenty-Nine",30=>"Thirty",31=>"Thirty-One",32=>"Thirty-Two",33=>"Thirty-Three",34=>"Thirty-Four",35=>"Thirty-Five",36=>"Thirty-Six",37=>"Thirty-Seven",38=>"Thirty-Eight",39=>"Thirty-Nine",40=>"Forty",41=>"Forty-One",42=>"Forty-Two",43=>"Forty-Three",44=>"Forty-Four",45=>"Forty-Five",46=>"Forty-Six",47=>"Forty-Seven",48=>"Forty-Eight",49=>"Forty-Nine",50=>"Fifty",51=>"Fifty-One",52=>"Fifty-Two",53=>"Fifty-Three",54=>"Fifty-Four",55=>"Fifty-Five",56=>"Fifty-Six",57=>"Fifty-Seven",58=>"Fifty-Eight",59=>"Fifty-Nine",60=>"Sixty",61=>"Sixty-One",62=>"Sixty-Two",63=>"Sixty-Three",64=>"Sixty-Four",65=>"Sixty-Five",66=>"Sixty-Six",67=>"Sixty-Seven",68=>"Sixty-Eight",69=>"Sixty-Nine",70=>"Seventy",71=>"Seventy-One",72=>"Seventy-Two",73=>"Seventy-Three",74=>"Seventy-Four",75=>"Seventy-Five",76=>"Seventy-Six",77=>"Seventy-Seven",78=>"Seventy-Eight",79=>"Seventy-Nine",80=>"Eighty",81=>"Eighty-One",82=>"Eighty-Two",83=>"Eighty-Three",84=>"Eighty-Four",85=>"Eighty-Five",86=>"Eighty-Six",87=>"Eighty-Seven",88=>"Eighty-Eight",89=>"Eighty-Nine",90=>"Ninety",91=>"Ninety-One",92=>"Ninety-Two",93=>"Ninety-Three",94=>"Ninety-Four",95=>"Ninety-Five",96=>"Ninety-Six",97=>"Ninety-Seven",98=>"Ninety-Eight",99=>"Ninety-Nine",100=>"One Hundred"); private $thousand = array("", "Thousand, ", "Million, ", "Billion, ", "Trillion, ", "Zillion, "); private $val, $currency0, $currency1; private $val_array, $dec_value, $dec_word, $num_value, $num_word; var $val_word; public function number_word($in_val = 0, $in_currency0 = "", $in_currency1 = "") { $this->val = $in_val; $this->currency0 = $in_currency0; $this->currency1 = $in_currency1; $this->val = abs(floatval(str_replace(",","",$this->val))); if ($this->val > 0) { $this->val = number_format($this->val, '2', ',', ','); $this->val_array = explode(",", $this->val); $this->dec_value = intval($this->val_array[count($this->val_array) - 1]); if ($this->dec_value > 0) { $this->dec_word = $this->word_array[$this->dec_value]." ".$this->currency1; } $t = 0; $this->num_word = ""; for ($i = count($this->val_array) - 2; $i >= 0; $i--) { if(is_infinite(intval($this->val_array[$i]))){ // Number is too big insert some kind of error handling here }else{ $this->num_value = intval($this->val_array[$i]); if ($this->num_value == 0) { $this->num_word = "".$this->num_word; } elseif (strlen($this->num_value."") <= 2) { $this->num_word = $this->word_array[$this->num_value]." ".$this->thousand[$t].$this->num_word; if ($i == 1) { $this->num_word = " and ".$this->num_word; } } else { $this->num_word = $this->word_array[substr($this->num_value, 0, 1)."00"]. (intval(substr($this->num_value, 1, 2)) > 0 ? " and " : "") .$this->word_array[intval(substr($this->num_value, 1, 2))]." ".$this->thousand[$t].$this->num_word; } } $t++; } if (!empty($this->num_word)) { $this->num_word .= " ".$this->currency0; } } $this->val_word = $this->num_word." ".$this->dec_word; } } ?> Code (markup):
I appreciate Your help so far and thanks for it, but after implemented the code it no different from the previous.