Home > Backend Development > PHP Tutorial > php实现将人民币金额转大写的方法类

php实现将人民币金额转大写的方法类

WBOY
Release: 2016-06-20 13:03:59
Original
1178 people have browsed it

工作中偶尔会碰到需要将人民币金额,也即阿拉伯数字转化为大写汉字的这种情况,下面是作者经过实践总结出来的方法,特此记录以防备忘。

<p>class Num2Cny{</p>  static $basical=array(0=>'零','壹','贰','叁','肆','伍','陆','柒','捌','玖');<br />  static $advanced=array(1=>'拾','佰','仟');<br />  public static function ParseNumber($number){<br />    $number=trim($number);<br />    if(!is_numeric($number)||intval($number)>999999999999) return 'error';<br />    if($number==0) return '零';<br />    if(strpos($number,'.')){<br />      $number=round($number,2);<br />      $data=explode('.',$number);<br />      $data[0]=self::int($data[0]);<br />      $data[1]=self::dec($data[1]);<br />      return $data[0].$data[1];<br />    }else{<br />      return self::int($number).'整';<br />    }<br />  }<br />  public static function int($number){<br />    $arr=array_reverse(str_split($number));<br />    $data='';<br />    $zero=false;<br />    $zero_num=0;<br />    foreach($arr as $k=>$v){<br />      $_chinese='';<br />      $zero=($v==0)?true:false;<br />      $x=$k%4;<br />      if($x && $zero && $zero_num>1)continue;<br />      switch($x){<br />        case 0:<br />          if($zero){<br />            $zero_num=0;<br />          }else{<br />            $_chinese=self::$basical[$v];<br />            $zero_num=1;<br />          }<br />          if($k==8){<br />            $_chinese.='亿';<br />          }elseif($k==4){<br />            $_chinese.='万';<br />          }<br />          break;  <br />        default:<br />          if($zero){<br />            if($zero_num==1){<br />              $_chinese=self::$basical[$v];<br />              $zero_num++;<br />            }<br />          }else{<br />            $_chinese=self::$basical[$v];<br />            $_chinese.=self::$advanced[$x];<br />          }<br />      }<br />      $data=$_chinese.$data;<br />    }<br />    return $data.'元';<br />  }<br />  public static function dec($number){<br />    if(strlen($number)<2) $number.='0';<br />    $arr=array_reverse(str_split($number));<br />    $data='';<br />    $zero_num=false;<br />    foreach($arr as $k=>$v){<br />      $zero=($v==0)?true:false;<br />      $_chinese='';<br />      if($k==0){<br />        if(!$zero){<br />          $_chinese=self::$basical[$v];<br />          $_chinese.='分';<br />          $zero_num=true;<br />        }<br />      }else{<br />        if($zero){<br />          if($zero_num){<br />            $_chinese=self::$basical[$v];<br />          }<br />        }else{<br />          $_chinese=self::$basical[$v];<br />          $_chinese.='角';<br />        }<br />      }<br />      $data=$_chinese.$data;<br />    }<br />    return $data;<br />  }<br /><p>}
Copy after login

使用过程也很简单,如下:

echo Num2Cny::ParseNumber(1234567.5);//www.scutephp.com

结果将输出:

壹佰贰拾叁万肆仟伍佰陆拾柒元伍角

相关:JS数字金额转大写金额方法


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template