• 将数字类型的金钱转化为大写


      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 } 
  • 相关阅读:
    2020-2021-1 20209307 《Linux内核原理与分析》第九周作业
    2020-2021-1 20209307 《Linux内核原理与分析》第八周作业
    2020-2021-1 20209307《Linux内核原理与分析》第七周作业
    2020-2021-1 20209307《Linux内核原理与分析》第六周作业
    2020-2021-1 20209307《Linux内核原理与分析》第五周作业
    2020-2021-1 20209307《Linux内核原理与分析》第四周作业
    2020-2021-1 20209307《Linux内核原理与分析》第三周作业
    2020-2021-1 20209307 《Linux内核原理与分析》第二周作业
    2020-2021-1 20209321《Linux内核原理与分析》第十一周作业
    2020-2021-1 Linux 《内核原理与分析》第九周作业
  • 原文地址:https://www.cnblogs.com/diguaer/p/5609785.html
Copyright © 2020-2023  润新知