Home > Backend Development > PHP Tutorial > Efficient RMB amount conversion program PHP version

Efficient RMB amount conversion program PHP version

WBOY
Release: 2016-07-25 08:43:23
Original
1233 people have browsed it
  1. //PHP version of the program for converting RMB amount to uppercase
  2. class Ext_Num2Cny{
  3. static $basical = array(0=>"zero","壹","谰","三", "四","五","鲁","旒","八","九");
  4. static $advanced=array(1=>"十","百","千");
  5. public static function ParseNumber($number){
  6. $number=trim($number);
  7. if ($number>999999999999) return "The number is too large to handle. Sorry!";
  8. if ($number==0) return "zero";
  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).'whole';
  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.='billion';
  39. }elseif($ k==4){
  40. $_chinese.='wan';
  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 .='fen';
  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.='angle';
  80. }
  81. }
  82. $data=$_chinese.$data;
  83. }
  84. return $data;
  85. }
  86. }
Copy code

PHP


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