欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-03-21 08:15:02
...
  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) $arr=array_reverse(str_split($number));
  60. $data='';
  61. $zero_num=false;
  62. foreach ($arr as $k=>$v){
  63. $zero=($v==0)?true:false;
  64. $_chinese='';
  65. if($k==0){
  66. if(!$zero){
  67. $_chinese=self::$basical[$v];
  68. $_chinese.='分';
  69. $zero_num=true;
  70. }
  71. }else{
  72. if($zero){
  73. if($zero_num){
  74. $_chinese=self::$basical[$v];
  75. }
  76. }else{
  77. $_chinese=self::$basical[$v];
  78. $_chinese.='角';
  79. }
  80. }
  81. $data=$_chinese.$data;
  82. }
  83. return $data;
  84. }
  85. }
复制代码

PHP