当前位置:Gxlcms > PHP教程 > 更加完善数字转换中文类

更加完善数字转换中文类

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

最近老是遇到数字转换中文的问题,写了个分享一下。大家多指教。
  1. /*
  2. * func 数字转换中文类
  3. * Author shuang
  4. * date 2012-08-17
  5. * email:shuangbrother@sina.com
  6. */
  7. class TransFormNumberNew{
  8. public $chinaData = array('1'=>'壹','2'=>'贰','3'=>'叁','4'=>'肆','5'=>'伍','6'=>'陆','7'=>'柒','8'=>'捌','9'=>'玖');
  9. public $chinaDataInt = array('1'=>'','2'=>'拾','3'=>'佰','4'=>'仟');
  10. public $chinaDataFloat = array('1'=>'角','2'=>'分');
  11. private $Intnumber; // string
  12. private $Floatnumber; // string
  13. public $error = array('0'=>'零','def'=>'数据格式不支持');
  14. public function __construct($intnumber,$floatnumber){
  15. $this->Intnumber = $intnumber;
  16. $this->Floatnumber = $floatnumber;
  17. }
  18. public function getTransInt(){
  19. /*如果数字是0或非数字字符返回错误提示*/
  20. if($this->Intnumber == 0){
  21. return $this->errorNotice('def');
  22. }
  23. if(!preg_match('/^\d+$/',$this->Intnumber)){
  24. return $this->errorNotice('def');
  25. }
  26. /*去除字符串开头是0的字符*/
  27. $this->dealIntZero();
  28. $data = array();
  29. /*把字符串分成4个一组*/
  30. $data = str_split(strrev($this->Intnumber),4);
  31. return $this->setTransInt($data);
  32. }
  33. public function getTransFloat(){
  34. return $this->setTransFloat($this->Floatnumber,strlen($this->Floatnumber));
  35. }
  36. private function dealIntZero(){
  37. $j = strlen($this->Intnumber);
  38. for($i=0;$i<$j;$i++){
  39. if($this->Intnumber{$i} != 0){
  40. $this->Intnumber = substr($this->Intnumber,$i,$j);
  41. break;
  42. }
  43. }
  44. }
  45. private function setTransInt($data){
  46. $str = '';
  47. $newArray = array();
  48. while(list($key,$val) = each($data)){
  49. $j = strlen($val);
  50. if($j < 4){
  51. /*如果字符串不够4位,我们用0补齐*/
  52. $val = str_pad($val, 4, "0", STR_PAD_RIGHT);
  53. }
  54. for($i=0;$i<$j;$i++){
  55. /*每四个字符串一循环;如果字符串为0,判断一下它的前一位是否为0,如果是0,不处理。不是0,我们用“零”补齐*/
  56. if($val{$i} == 0){
  57. if($val{$i-1}){
  58. $newArray[$key][] = '零';
  59. }
  60. }else{
  61. $newArray[$key][] = $this->chinaData[$val{$i}].$this->chinaDataInt[$i+1];
  62. }
  63. }
  64. }
  65. unset($data,$key,$val);
  66. /*上面的循环我们已经得到了转换成中文的数组;下面我排列即可*/
  67. foreach(array_reverse($newArray,true) as $key=>$val){
  68. if($key == 0){
  69. $str .= implode('',array_reverse($val));
  70. }
  71. if($key%2 == 1){
  72. $j = floor($key/2);
  73. if($j == 0){
  74. $str .= implode('',array_reverse($val)).'万';
  75. }else{
  76. $str .= implode('',array_reverse($val)).'万'.str_pad('',3*$j,'亿');
  77. }
  78. }
  79. if($key%2 == 0 && $key != 0){
  80. if($key/2 > 1){
  81. $str .= implode('',array_reverse($val)).'万万'.str_pad('',3*(floor($key/2)-1),'亿');
  82. }else{
  83. $str .= implode('',array_reverse($val)).'亿';
  84. }
  85. }
  86. }
  87. unset($newArray,$key,$val,$j);
  88. return $str;
  89. }
  90. //紧支持两位小数
  91. private function setTransFloat($floatData,$pos){
  92. if($pos > 2){
  93. return $this->errorNotice('def');
  94. }
  95. if($floatData{0} == 0){
  96. $data[] = '零';
  97. }else{
  98. $data[] = $this->chinaData[$floatData{0}].$this->chinaDataFloat[1];
  99. }
  100. if($floatData一念之间 != 0 ){
  101. $data[] = $this->chinaData[$floatData一念之间].$this->chinaDataFloat[2];
  102. }
  103. return implode('',$data);
  104. }
  105. public function errorNotice($error){
  106. return $this->error[$error];
  107. }
  108. }
  109. $num = new TransFormNumberNew('450252352007760006601000300','80');
  110. echo $num->getTransInt();
  111. echo $num->getTransFloat();

人气教程排行