Rumah  >  Artikel  >  pembangunan bahagian belakang  >  更加完善数字转换中文类

更加完善数字转换中文类

WBOY
WBOYasal
2016-07-25 09:05:55938semak imbas
最近老是遇到数字转换中文的问题,写了个分享一下。大家多指教。
  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 if($this->Intnumber{$i} != 0){
  39. $this->Intnumber = substr($this->Intnumber,$i,$j);
  40. break;
  41. }
  42. }
  43. }
  44. private function setTransInt($data){
  45. $str = '';
  46. $newArray = array();
  47. while(list($key,$val) = each($data)){
  48. $j = strlen($val);
  49. if($j /*如果字符串不够4位,我们用0补齐*/
  50. $val = str_pad($val, 4, "0", STR_PAD_RIGHT);
  51. }
  52. for($i=0;$i /*每四个字符串一循环;如果字符串为0,判断一下它的前一位是否为0,如果是0,不处理。不是0,我们用“零”补齐*/
  53. if($val{$i} == 0){
  54. if($val{$i-1}){
  55. $newArray[$key][] = '零';
  56. }
  57. }else{
  58. $newArray[$key][] = $this->chinaData[$val{$i}].$this->chinaDataInt[$i+1];
  59. }
  60. }
  61. }
  62. unset($data,$key,$val);
  63. /*上面的循环我们已经得到了转换成中文的数组;下面我排列即可*/
  64. foreach(array_reverse($newArray,true) as $key=>$val){
  65. if($key == 0){
  66. $str .= implode('',array_reverse($val));
  67. }
  68. if($key%2 == 1){
  69. $j = floor($key/2);
  70. if($j == 0){
  71. $str .= implode('',array_reverse($val)).'万';
  72. }else{
  73. $str .= implode('',array_reverse($val)).'万'.str_pad('',3*$j,'亿');
  74. }
  75. }
  76. if($key%2 == 0 && $key != 0){
  77. if($key/2 > 1){
  78. $str .= implode('',array_reverse($val)).'万万'.str_pad('',3*(floor($key/2)-1),'亿');
  79. }else{
  80. $str .= implode('',array_reverse($val)).'亿';
  81. }
  82. }
  83. }
  84. unset($newArray,$key,$val,$j);
  85. return $str;
  86. }
  87. //紧支持两位小数
  88. private function setTransFloat($floatData,$pos){
  89. if($pos > 2){
  90. return $this->errorNotice('def');
  91. }
  92. if($floatData{0} == 0){
  93. $data[] = '零';
  94. }else{
  95. $data[] = $this->chinaData[$floatData{0}].$this->chinaDataFloat[1];
  96. }
  97. if($floatData一念之间 != 0 ){
  98. $data[] = $this->chinaData[$floatData一念之间].$this->chinaDataFloat[2];
  99. }
  100. return implode('',$data);
  101. }
  102. public function errorNotice($error){
  103. return $this->error[$error];
  104. }
  105. }
  106. $num = new TransFormNumberNew('450252352007760006601000300','80');
  107. echo $num->getTransInt();
  108. echo $num->getTransFloat();
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn