Home  >  Article  >  php教程  >  数字转换中文

数字转换中文

PHP中文网
PHP中文网Original
2016-05-25 17:10:211163browse

php代码

<?php

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

    public function __construct($intnumber,$floatnumber){
        $this->Intnumber = $intnumber;
        $this->Floatnumber = $floatnumber;
    }
    /*13位整数*/
    public function getTransInt(){
      $this->Intnumber = intval($this->Intnumber);
      $len = strlen($this->Intnumber);
      if($len > 13){
          return $this->errorNotice(1);
      }
      if($this->Intnumber == 0){
          return $this->errorNotice(&#39;def&#39;);
      }
      $intData = strrev(substr($this->Intnumber,0,$len));
      $data = $this->setTransInt($intData,$len);
      krsort($data);
      $str = rtrim(join($data,&#39;&#39;),&#39;零&#39;);
      if(substr($this->Intnumber,$len-1,1) == 0){
          $str.=&#39;元&#39;;
      }
      unset($data);
      return $str;
   }
   public function getTransFloat(){
       return $this->setTransFloat($this->Floatnumber,strlen($this->Floatnumber));
   }
   private function setTransInt($intData,$pos){
      for($i=0;$i<$pos;$i++){
              $oneData = substr($intData,$i,1);
              if(($i == 4 || ($i%8) == 4) && $oneData == 0){
                  if(substr($intData,$i+1,1) == 0 && substr($intData,$i+2,1) == 0 && substr($intData,$i+3,1) == 0){
                      if(substr($intData,$i-1,1) == 0){
                          continue;
                      }else{
                          $data[] = &#39;零&#39;;
                      }
                  }else{
                      $data[] = &#39;万&#39;;
                      continue;
                  }
                  continue;
              }
              if($i%8 == 0 && $i != 0 && $oneData == 0){
                  $data[] = &#39;亿&#39;;
              }
              if($oneData == 0){
                  if(($i == 4 || $i == 7)){
                      if((substr($intData,$i-1,1) ==0)){
                            continue;
                      }else{
                          $data[] = &#39;零&#39;;
                      }
                  }else{
                      if(substr($intData,$i-1,1) !=0 && $i !=3 && $i != 8){
                          $data[] = &#39;零&#39;;
                      }else{
                          if($i == 3 && substr($intData,$i,1) == 0 && substr($intData,$i-1,1) != 0){
                              $data[] = &#39;零&#39;;
                          }
                      }
                  }
              }else{
                $data[] = $this->chinaData[$oneData].$this->chinaDataInt[$i+1];
              }

          }
         unset($intData,$oneData,$i);
         return $data;
   }
   //紧支持两位小数
   private function setTransFloat($floatData,$pos){
        if($pos > 2){
            return $this->errorNotice(&#39;def&#39;);
        }
        if($floatData{0} == 0){
            $data[] = &#39;零&#39;;
        }else{
            $data[] = $this->chinaData[$floatData{0}].$this->chinaDataFloat[1];
        }
        if($floatData{1} != 0 ){
            $data[] = $this->chinaData[$floatData{1}].$this->chinaDataFloat[2];
        }
        return implode(&#39;&#39;,$data);
   }
  public function errorNotice($error){
      return $this->error[$error];
  }
}

$num = new TransFormNumber(&#39;100228030&#39;,&#39;80&#39;);
echo $num->getTransInt();
echo $num->getTransFloat();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn