当前位置:Gxlcms > PHP教程 > 数字转换中文

数字转换中文

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

<无详细内容>
  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<$pos;$i++){
  37. $oneData = substr($intData,$i,1);
  38. if(($i == 4 || ($i%8) == 4) && $oneData == 0){
  39. if(substr($intData,$i+1,1) == 0 && substr($intData,$i+2,1) == 0 && substr($intData,$i+3,1) == 0){
  40. if(substr($intData,$i-1,1) == 0){
  41. continue;
  42. }else{
  43. $data[] = '零';
  44. }
  45. }else{
  46. $data[] = '万';
  47. continue;
  48. }
  49. continue;
  50. }
  51. if($i%8 == 0 && $i != 0 && $oneData == 0){
  52. $data[] = '亿';
  53. }
  54. if($oneData == 0){
  55. if(($i == 4 || $i == 7)){
  56. if((substr($intData,$i-1,1) ==0)){
  57. continue;
  58. }else{
  59. $data[] = '零';
  60. }
  61. }else{
  62. if(substr($intData,$i-1,1) !=0 && $i !=3 && $i != 8){
  63. $data[] = '零';
  64. }else{
  65. if($i == 3 && substr($intData,$i,1) == 0 && substr($intData,$i-1,1) != 0){
  66. $data[] = '零';
  67. }
  68. }
  69. }
  70. }else{
  71. $data[] = $this->chinaData[$oneData].$this->chinaDataInt[$i+1];
  72. }
  73. }
  74. unset($intData,$oneData,$i);
  75. return $data;
  76. }
  77. //紧支持两位小数
  78. private function setTransFloat($floatData,$pos){
  79. if($pos > 2){
  80. return $this->errorNotice('def');
  81. }
  82. if($floatData{0} == 0){
  83. $data[] = '零';
  84. }else{
  85. $data[] = $this->chinaData[$floatData{0}].$this->chinaDataFloat[1];
  86. }
  87. if($floatDataBB彪 != 0 ){
  88. $data[] = $this->chinaData[$floatDataBB彪].$this->chinaDataFloat[2];
  89. }
  90. return implode('',$data);
  91. }
  92. public function errorNotice($error){
  93. return $this->error[$error];
  94. }
  95. }
  96. $num = new TransFormNumber('100228030','80');
  97. echo $num->getTransInt();
  98. echo $num->getTransFloat();

人气教程排行