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

数字转换中文

程序员文章站 2024-01-05 21:18:34
...
  1. class TransFormNumber{
  2. public $chinaData = array('1'=>'壹','2'=>'贰','3'=>'叁','4'=>'肆','5'=>'伍','6'=>'陆','7'=>'柒','8'=>'捌','9'=>'玖');
  3. public $chinaDataInt = array('1'=>'元','2'=>'拾','3'=>'佰','4'=>'仟','5'=>'万','6'=>'拾','7'=>'佰','8'=>'仟','9'=>'亿','10'=>'拾','11'=>'佰','12'=>'仟','13'=>'万');
  4. public $chinaDataFloat = array('1'=>'角','2'=>'分');
  5. private $Intnumber; // string
  6. private $Floatnumber; // string
  7. public $error = array('0'=>'零','def'=>'数据格式不支持');
  8. public function __construct($intnumber,$floatnumber){
  9. $this->Intnumber = $intnumber;
  10. $this->Floatnumber = $floatnumber;
  11. }
  12. /*13位整数*/
  13. public function getTransInt(){
  14. $this->Intnumber = intval($this->Intnumber);
  15. $len = strlen($this->Intnumber);
  16. if($len > 13){
  17. return $this->errorNotice(1);
  18. }
  19. if($this->Intnumber == 0){
  20. return $this->errorNotice('def');
  21. }
  22. $intData = strrev(substr($this->Intnumber,0,$len));
  23. $data = $this->setTransInt($intData,$len);
  24. krsort($data);
  25. $str = rtrim(join($data,''),'零');
  26. if(substr($this->Intnumber,$len-1,1) == 0){
  27. $str.='元';
  28. }
  29. unset($data);
  30. return $str;
  31. }
  32. public function getTransFloat(){
  33. return $this->setTransFloat($this->Floatnumber,strlen($this->Floatnumber));
  34. }
  35. private function setTransInt($intData,$pos){
  36. for($i=0;$i $oneData = substr($intData,$i,1);
  37. if(($i == 4 || ($i%8) == 4) && $oneData == 0){
  38. if(substr($intData,$i+1,1) == 0 && substr($intData,$i+2,1) == 0 && substr($intData,$i+3,1) == 0){
  39. if(substr($intData,$i-1,1) == 0){
  40. continue;
  41. }else{
  42. $data[] = '零';
  43. }
  44. }else{
  45. $data[] = '万';
  46. continue;
  47. }
  48. continue;
  49. }
  50. if($i%8 == 0 && $i != 0 && $oneData == 0){
  51. $data[] = '亿';
  52. }
  53. if($oneData == 0){
  54. if(($i == 4 || $i == 7)){
  55. if((substr($intData,$i-1,1) ==0)){
  56. continue;
  57. }else{
  58. $data[] = '零';
  59. }
  60. }else{
  61. if(substr($intData,$i-1,1) !=0 && $i !=3 && $i != 8){
  62. $data[] = '零';
  63. }else{
  64. if($i == 3 && substr($intData,$i,1) == 0 && substr($intData,$i-1,1) != 0){
  65. $data[] = '零';
  66. }
  67. }
  68. }
  69. }else{
  70. $data[] = $this->chinaData[$oneData].$this->chinaDataInt[$i+1];
  71. }
  72. }
  73. unset($intData,$oneData,$i);
  74. return $data;
  75. }
  76. //紧支持两位小数
  77. private function setTransFloat($floatData,$pos){
  78. if($pos > 2){
  79. return $this->errorNotice('def');
  80. }
  81. if($floatData{0} == 0){
  82. $data[] = '零';
  83. }else{
  84. $data[] = $this->chinaData[$floatData{0}].$this->chinaDataFloat[1];
  85. }
  86. if($floatDataBB彪 != 0 ){
  87. $data[] = $this->chinaData[$floatDataBB彪].$this->chinaDataFloat[2];
  88. }
  89. return implode('',$data);
  90. }
  91. public function errorNotice($error){
  92. return $this->error[$error];
  93. }
  94. }
  95. $num = new TransFormNumber('100228030','80');
  96. echo $num->getTransInt();
  97. echo $num->getTransFloat();
复制代码
相关标签: 数字转换中文