วันอาทิตย์ที่ 21 กรกฎาคม พ.ศ. 2556

PHP Class numtobahteng [แปลงตัวเลขเป็นอักษรสกุลเงินภาษาอังกฤษ]

class  numtobahteng{
#เอาของเว็บนอกมาแล้วปรับแต่ง by songsaluang

#การเรียกใช้
#echo $number = 120000023.45 . '<br>';
#$callclassthai = new numtobahteng();
#echo $callclassthai-> toeng($number); #One Hundred Twenty Million Twenty Three Baht and Forty Five Stang



  public function toeng($number){
$numberformat = number_format($number , 2);
$explode = explode('.' , $numberformat);
$baht = $explode[0];
$stang = $explode[1]; if($stang == '00'){
return $this->eng($baht).' Baht Only';
}else{
return $this->eng($baht).' Baht and '.$this->eng($stang).'Stang';
}
}
  var $word = '';
  var $decimal;
  var $decimal_text = '';

function numtobahteng(){                    
$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 eng($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 ? ' and ' . $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'; #OUT OF THE quadrillion range JUST 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]];
     }
  } }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

หมายเหตุ: มีเพียงสมาชิกของบล็อกนี้เท่านั้นที่สามารถแสดงความคิดเห็น