首頁  >  文章  >  後端開發  >  更加完善數位轉換中文類

更加完善數位轉換中文類

WBOY
WBOY原創
2016-07-25 09:05:55895瀏覽


最近老是遇到數字轉換中文的問題,寫了個分享一下。大家多指教。
  1. /*
  2. * func 數位轉換中文類別
  3. * Author shuang
  4. * date 2012-08-17
  5. * email:shuangbrmail:shuang.
  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(list(> $newArray = array();
  48. while(list(list( $key,$val) = each($data)){
  49. $j = strlen($val);
  50. if($j /*如果字串不夠4位,我們用0補齊*/
  51. $val = str_pad($val, 4, "0", STR_PAD_RIGHT);
  52. }
  53. for($i=0;$i /*每四個字串一循環;如果字串為0,判斷一下它的前一位是否為0,如果是0,不處理。不是0,我們用「零」補齊*/
  54. if($val{$i} == 0){
  55. if($val{$i-1}){
  56. $newArray[$ key][] = '零';
  57. }
  58. }else{
  59. $newArray[$key][] = $this->chinaData[$val{$i}].$this->chinaDataInt [$i+1];
  60. }
  61. }
  62. }
  63. unset($data,$key,$val);
  64. /*上面的循環我們已經得到了轉換成中文的陣列;下面我排列即可*/
  65. foreach(array_reverse($newArray,true) as $key=>$val){
  66. if($key == 0){
  67. $str .= implode ('',array_reverse($val));
  68. }
  69. if($key%2 == 1){
  70. $j = floor($key/2);
  71. if($j == 0){
  72. $str .= implode('',array_reverse($val)).'萬';
  73. }else{
  74. $str .= implode('',array_reverse($val )).'萬'.str_pad('',3*$j,'億');
  75. }
  76. }
  77. if($key%2 == 0 && $key != 0){
  78. if($key/2 > 1){
  79. $str .= implode('',array_reverse($val)).'萬萬'.str_pad('',3*(floor($key/ 2)-1),'億');
  80. }else{
  81. $str .= implode('',array_reverse($val)).'億';
  82. }
  83. }
  84. }
  85. unset($newArray,$key,$val,$j);
  86. return $str;
  87. }
  88. //緊支援兩位小數
  89. private function setTransFloat($floatData ,$pos){
  90. if($pos > 2){
  91. return $this->errorNotice('def');
  92. }
  93. if($floatData{0} == 0){
  94. $data[] = '零';
  95. }else{
  96. $data[] = $this->chinaData[$floatData{0}].$this->chinaDataFloat[1];
  97. }
  98. if($floatData一念之間!= 0 ){
  99. $data[] = $this->chinaData[$floatData一念之間].$this->chinaDataFloat[2];
  100. }
  101. return implode('',$data);
  102. }
  103. public function errorNotice($error){
  104. return $this->error[$error];
  105. }
  106. }
  107. $num = new TransFormNumberNew('45025235200776000601000300','80');
  108. echo $num->getTransInt();
echo $num->get複製程式碼
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn