More perfect digital conversion Chinese category

WBOY
Release: 2016-07-25 09:05:55
Original
942 people have browsed it
Recently, I keep encountering the problem of digital conversion into Chinese, so I wrote a post to share it. Please give me your advice.
  1. /*
  2. * func number conversion Chinese class
  3. * Author shuang
  4. * date 2012-08-17
  5. * email:shuangbrother@sina.com
  6. */
  7. class TransFormNumberNew{
  8. public $chinaData = array(' 1'=>'one','2'=>'two','3'=>'three','4'=>'four','5'=>'five',' 6'=>'Lu','7'=>'旒','8'=>'捌','9'=>'玖');
  9. public $chinaDataInt = array('1' =>'','2'=>'Shi','3'=>'䰰','4'=>'仟');
  10. public $chinaDataFloat = array('1'=> 'angle','2'=>'fen');
  11. private $Intnumber; // string
  12. private $Floatnumber; // string
  13. public $error = array('0'=>'zero','def '=>'Data format not supported');
  14. public function __construct($intnumber,$floatnumber){
  15. $this->Intnumber = $intnumber;
  16. $this->Floatnumber = $floatnumber;
  17. }
  18. public function getTransInt(){
  19. /*If the number is 0 or a non-numeric character, return an error message*/
  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. /*Remove strings that start with 0 characters*/
  27. $this->dealIntZero();
  28. $data = array();
  29. /*Divide the string into groups of 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. /*If the string is not enough 4 digits, we fill it with 0*/
  52. $val = str_pad($val, 4, "0", STR_PAD_RIGHT);
  53. }
  54. for($ i=0;$i<$j;$i++){
  55. /*Loop for every four strings; if the string is 0, check whether its previous digit is 0. If it is 0, do not process it. Not 0, we pad it with "zero" */
  56. if($val{$i} == 0){
  57. if($val{$i-1}){
  58. $newArray[$key][] = ' zero';
  59. }
  60. }else{
  61. $newArray[$key][] = $this->chinaData[$val{$i}].$this->chinaDataInt[$i+1];
  62. }
  63. }
  64. }
  65. unset($data,$key,$val);
  66. /*In the above loop, we have already obtained the array converted into Chinese; let me arrange it below*/
  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,'Billion');
  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),'Billion');
  82. }else{
  83. $str .= implode('',array_reverse($val)).'Billion';
  84. }
  85. }
  86. }
  87. unset($newArray, $key,$val,$j);
  88. return $str;
  89. }
  90. //Tightly supports two decimal places
  91. private function setTransFloat($floatData,$pos){
  92. if($pos > 2){
  93. return $ this->errorNotice('def');
  94. }
  95. if($floatData{0} == 0){
  96. $data[] = 'zero';
  97. }else{
  98. $data[] = $this-> ;chinaData[$floatData{0}].$this->chinaDataFloat[1];
  99. }
  100. if($floatData!= 0 ){
  101. $data[] = $this->chinaData[$floatData In a flash].$this->chinaDataFloat[2];
  102. }
  103. return implode('',$data);
  104. }
  105. public function errorNotice($error){
  106. return $this->error[$error ];}}
  107. }
  108. $ num = New Transformnumbernew ('450252352007760006601000300', '80'); ; gettransfloat ();
  109. Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template