zoukankan      html  css  js  c++  java
  • 将数字类型的金钱转化为大写

      1 class MoneyUtil
      2 {
      3     static $basical = array(0=>"零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
      4     static $advanced=array(1=>"拾","佰","仟");
      5 
      6     /**
      7      *转为文字
      8      * @param $number
      9      * @return string
     10      */
     11     public static function ParseNumber($number)
     12     {
     13         $number=trim($number);
     14         if ($number>999999999999) return false;
     15         if ($number==0) return "零元";
     16         if(strpos($number,'.')){
     17             $number=round($number,2);
     18             $data=explode(".",$number);
     19             $data[0]=self::int($data[0]);
     20             $data[1]=self::dec($data[1]);
     21             return $data[0].$data[1];
     22         }else{
     23             return self::int($number).'整';
     24         }
     25     }
     26 
     27     private  static function int($number)
     28     {
     29         $arr=array_reverse(str_split($number));
     30         $data='';
     31         $zero=false;
     32         $zero_num=0;
     33         foreach ($arr as $k=>$v)
     34         {
     35             $_chinese='';
     36             $zero=($v==0)?true:false;
     37             $x=$k%4;
     38             if($x && $zero && $zero_num>1)continue;
     39             switch ($x){
     40                 case 0:
     41                     if($zero){
     42                         $zero_num=0;
     43                     }else{
     44                         $_chinese=self::$basical[$v];
     45                         $zero_num=1;
     46                     }
     47                     if($k==8){
     48                         $_chinese.='亿';
     49                     }elseif($k==4){
     50                         $_chinese.='万';
     51                     }
     52                     break;
     53                 default:
     54                     if($zero){
     55                         if($zero_num==1){
     56                             $_chinese=self::$basical[$v];
     57                             $zero_num++;
     58                         }
     59                     }else{
     60                         $_chinese=self::$basical[$v];
     61                         $_chinese.=self::$advanced[$x];
     62                     }
     63             }
     64             $data=$_chinese.$data;
     65         }
     66         if(empty($data))
     67         {
     68             $data = '零';
     69         }
     70         return $data.'元';
     71     }
     72 
     73     private static function dec($number)
     74     {
     75         if(strlen($number)<2) $number.='0';
     76         $arr=array_reverse(str_split($number));
     77         $data='';
     78         $zero_num=false;
     79         foreach ($arr as $k=>$v){
     80             $zero=($v==0)?true:false;
     81             $_chinese='';
     82             if($k==0){
     83                 if(!$zero){
     84                     $_chinese=self::$basical[$v];
     85                     $_chinese.='分';
     86                     $zero_num=true;
     87                 }
     88             }else{
     89                 if($zero){
     90                     if($zero_num){
     91                         $_chinese=self::$basical[$v];
     92                     }
     93                 }else{
     94                     $_chinese=self::$basical[$v];
     95                     $_chinese.='角';
     96                 }
     97             }
     98             $data=$_chinese.$data;
     99         }
    100         return $data;
    101     }
    102 } 
  • 相关阅读:
    希尔排序
    代理模式
    快速排序
    插入排序
    各种排序算法的稳定性和复杂度
    简单选择排序
    冒泡排序
    流程图
    PLAY学习【未完】
    项目之maven心得
  • 原文地址:https://www.cnblogs.com/diguaer/p/5609785.html
Copyright © 2011-2022 走看看