Home > Article > Backend Development > A supplement to the class for converting numbers into Chinese written by Alpha.Z. I was bored last night_PHP Tutorial
Removed some redundant zeros "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); //var $basical=array(0=>"zero","one","two","three","four","五","鲁","七","八","九" ); var $advanced=array(1=>"十","百","千"); //var $advanced=array(1=>"十","百","千"); var $ top=array(1=>"10,000","100 million"); var $level; // Use 4 digits as one level // First implement the conversion function of numbers below 10,000 ParseNumber($number) { if ($number >999999999999) // Can only process up to 100 billion. return "The number is too large to handle. Sorry!"; if ($number==0) return "zero"; for($this->level=0;$number>0.0001;$this->level++,$number= floor($number / 10000)) { // For Chinese, it should be a group of 4 digits. // The four variables correspond to ones, tens, hundreds, and thousands respectively. $n1=substr($number,-1,1); if($number>9) $n2=substr($number,-2,1); else $n2=0; if($number>99) $n3 =substr($number,-3,1); else $n3=0; if($number>999) $n4=substr($number,-4,1); else $n4=0; if($n4) $parsed[$this->level].=$this->basical[$n4].$this->advanced[3]; else if(($number/10000)>=1) // Thousands digit is 0, If the value is greater than 9999 $parsed[$this->level].="zero"; if($n3) $parsed[$this->level].=$this->basical[$n3].$this-> advanced[2]; else if(!ereg("zero$",$parsed[$this->level]) && ($number / 1000)>=1) // There are no two consecutive "zeros" $ parsed[$this->level].="zero"; if($n2) $parsed[$this->level].=$this->basical[$n2].$this->advanced[1]; else if(!ereg("zero$",$parsed[$this->level]) && ($number / 100)>=1) // There will be no two consecutive "zeros" $parsed[$this-> level].="zero"; if($n1) $parsed[$this->level].=$this->basical[$n1]; if($parsed[$this->level]!="zero" ) { if(ereg("zero$",$parsed[$this->level])) $parsed[$this->level]=substr($parsed[$this->level],0,strlen($parsed [$this->level])-2); if($this->level>0) $result=$parsed[$this->level].$this->top[$this->level].$result ; else $result=$parsed[$this->level].$result; } } //for($this->level-=1;$this->level>=0;$this->level--) //{ // $result.=$parsed[$this->level].$this->top[$this->level]; //} //if(ereg("zero$",$result)) // $result=substr($result,0,strlen($result)-2); return $result; } }; $c=new ChineseNumber(); echo $c->ParseNumber(100000000001); ?>