更加完善数字转换中文类

字体大小: 中小 标准 ->行高大小: 标准

/*
 * func 数字转换中文类
 * Author shuang
 * date 2012-08-17
 * email:shuangbrother@sina.com
 */

class TransFormNumberNew{
    public $chinaData = array('1'=>'壹','2'=>'贰','3'=>'叁','4'=>'肆','5'=>'伍','6'=>'陆','7'=>'柒','8'=>'捌','9'=>'玖');
    public $chinaDataInt = array('1'=>'','2'=>'拾','3'=>'佰','4'=>'仟');
    public $chinaDataFloat = array('1'=>'角','2'=>'分');
    private $Intnumber; // string
    private $Floatnumber; // string
    public $error = array('0'=>'零','def'=>'数据格式不支持');

    public function __construct($intnumber,$floatnumber){
        $this->Intnumber = $intnumber;
        $this->Floatnumber = $floatnumber;
    }
    public function getTransInt(){
      /*如果数字是0或非数字字符返回错误提示*/
      if($this->Intnumber == 0){
          return $this->errorNotice('def');
      }
      if(!preg_match('/^\d+$/',$this->Intnumber)){
          return $this->errorNotice('def');
      }
      /*去除字符串开头是0的字符*/
      $this->dealIntZero();
     
      $data = array();
      /*把字符串分成4个一组*/
      $data = str_split(strrev($this->Intnumber),4);
      return $this->setTransInt($data);
   }
   public function getTransFloat(){
       return $this->setTransFloat($this->Floatnumber,strlen($this->Floatnumber));
   }
   private function dealIntZero(){
       $j = strlen($this->Intnumber);
       for($i=0;$i<$j;$i++){
           if($this->Intnumber{$i} != 0){
               $this->Intnumber = substr($this->Intnumber,$i,$j);
               break;
           }
       }
   }
   private function setTransInt($data){
      $str = '';
      $newArray = array();
      while(list($key,$val) = each($data)){
          $j = strlen($val);
          if($j < 4){
              /*如果字符串不够4位,我们用0补齐*/
              $val = str_pad($val, 4, "0", STR_PAD_RIGHT);
          }
          for($i=0;$i<$j;$i++){
             /*每四个字符串一循环;如果字符串为0,判断一下它的前一位是否为0,如果是0,不处理。不是0,我们用“零”补齐*/
             if($val{$i} == 0){
                if($val{$i-1}){
                    $newArray[$key][] = '零';
                }
             }else{
                $newArray[$key][] = $this->chinaData[$val{$i}].$this->chinaDataInt[$i+1];
             }
          }
      }
      unset($data,$key,$val);
      /*上面的循环我们已经得到了转换成中文的数组;下面我排列即可*/
      foreach(array_reverse($newArray,true) as $key=>$val){
          if($key == 0){
              $str .= implode('',array_reverse($val));
          }
          if($key%2 == 1){
              $j = floor($key/2);
              if($j == 0){
                $str .= implode('',array_reverse($val)).'万';
              }else{
                 $str .= implode('',array_reverse($val)).'万'.str_pad('',3*$j,'亿');
              }
          }
          if($key%2 == 0 && $key != 0){
              if($key/2 > 1){
                  $str .= implode('',array_reverse($val)).'万万'.str_pad('',3*(floor($key/2)-1),'亿');
              }else{
                $str .= implode('',array_reverse($val)).'亿';
              }
          }
      }
      unset($newArray,$key,$val,$j);
      return $str;
   }
   //紧支持两位小数
   private function setTransFloat($floatData,$pos){
        if($pos > 2){
            return $this->errorNotice('def');
        }
        if($floatData{0} == 0){
            $data[] = '零';
        }else{
            $data[] = $this->chinaData[$floatData{0}].$this->chinaDataFloat[1];
        }
        if($floatData{1} != 0 ){
            $data[] = $this->chinaData[$floatData{1}].$this->chinaDataFloat[2];
        }
        return implode('',$data);
   }
    public function errorNotice($error){
      return $this->error[$error];
    }
}
$num = new TransFormNumberNew('450252352007760006601000300','80');
echo $num->getTransInt();
echo $num->getTransFloat();

此文章由 www.phpgz.com 收集整理 ,地址为: http://www.phpgz.com/htmls/29919.html

大屏阅读,大屏评论.