"拾","佰","仟"); public static function ParseNumber($">
当前位置:Gxlcms > PHP教程 > 高效率的人民币金额转大写程序PHP版

高效率的人民币金额转大写程序PHP版

时间:2021-07-01 10:21:17 帮助过:32人阅读

  1. //人民币金额转大写程序PHP版
  2. class Ext_Num2Cny{
  3. static $basical = array(0=>"零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
  4. static $advanced=array(1=>"拾","佰","仟");
  5. public static function ParseNumber($number){
  6. $number=trim($number);
  7. if ($number>999999999999) return "数字太大,无法处理。抱歉!";
  8. if ($number==0) return "零";
  9. if(strpos($number,'.')){
  10. $number=round($number,2);
  11. $data=explode(".",$number);
  12. $data[0]=self::int($data[0]);
  13. $data[1]=self::dec($data[1]);
  14. return $data[0].$data[1];
  15. }else{
  16. return self::int($number).'整';
  17. }
  18. }
  19. public static function int($number){
  20. $arr=array_reverse(str_split($number));
  21. $data='';
  22. $zero=false;
  23. $zero_num=0;
  24. foreach ($arr as $k=>$v){
  25. $_chinese='';
  26. $zero=($v==0)?true:false;
  27. $x=$k%4;
  28. if($x && $zero && $zero_num>1)continue;
  29. switch ($x){
  30. case 0:
  31. if($zero){
  32. $zero_num=0;
  33. }else{
  34. $_chinese=self::$basical[$v];
  35. $zero_num=1;
  36. }
  37. if($k==8){
  38. $_chinese.='亿';
  39. }elseif($k==4){
  40. $_chinese.='万';
  41. }
  42. break;
  43. default:
  44. if($zero){
  45. if($zero_num==1){
  46. $_chinese=self::$basical[$v];
  47. $zero_num++;
  48. }
  49. }else{
  50. $_chinese=self::$basical[$v];
  51. $_chinese.=self::$advanced[$x];
  52. }
  53. }
  54. $data=$_chinese.$data;
  55. }
  56. return $data.'元';
  57. }
  58. public static function dec($number){
  59. if(strlen($number)<2) $number.='0';
  60. $arr=array_reverse(str_split($number));
  61. $data='';
  62. $zero_num=false;
  63. foreach ($arr as $k=>$v){
  64. $zero=($v==0)?true:false;
  65. $_chinese='';
  66. if($k==0){
  67. if(!$zero){
  68. $_chinese=self::$basical[$v];
  69. $_chinese.='分';
  70. $zero_num=true;
  71. }
  72. }else{
  73. if($zero){
  74. if($zero_num){
  75. $_chinese=self::$basical[$v];
  76. }
  77. }else{
  78. $_chinese=self::$basical[$v];
  79. $_chinese.='角';
  80. }
  81. }
  82. $data=$_chinese.$data;
  83. }
  84. return $data;
  85. }
  86. }

PHP

人气教程排行