Recently, I keep encountering the problem of digital conversion into Chinese, so I wrote a post to share it. Please give me your advice.
- /*
- * func number conversion Chinese class
- * Author shuang
- * date 2012-08-17
- * email:shuangbrother@sina.com
- */
-
- class TransFormNumberNew{
- public $chinaData = array(' 1'=>'one','2'=>'two','3'=>'three','4'=>'four','5'=>'five',' 6'=>'Lu','7'=>'旒','8'=>'捌','9'=>'玖');
- public $chinaDataInt = array('1' =>'','2'=>'Shi','3'=>'䰰','4'=>'仟');
- public $chinaDataFloat = array('1'=> 'angle','2'=>'fen');
- private $Intnumber; // string
- private $Floatnumber; // string
- public $error = array('0'=>'zero','def '=>'Data format not supported');
-
- public function __construct($intnumber,$floatnumber){
- $this->Intnumber = $intnumber;
- $this->Floatnumber = $floatnumber;
- }
- public function getTransInt(){
- /*If the number is 0 or a non-numeric character, return an error message*/
- if($this->Intnumber == 0){
- return $this->errorNotice('def');
- }
- if(!preg_match('/^d+$/',$this->Intnumber)){
- return $this->errorNotice('def');
- }
- /*Remove strings that start with 0 characters*/
- $this->dealIntZero();
-
- $data = array();
- /*Divide the string into groups of 4*/
- $data = str_split(strrev($this->Intnumber ),4);
- return $this->setTransInt($data);
- }
- public function getTransFloat(){
- return $this->setTransFloat($this->Floatnumber,strlen($this-> Floatnumber));
- }
- private function dealIntZero(){
- $j = strlen($this->Intnumber);
- for($i=0;$i<$j;$i++){
- if($this ->Intnumber{$i} != 0){
- $this->Intnumber = substr($this->Intnumber,$i,$j);
- break;
- }
- }
- }
- private function setTransInt ($data){
- $str = '';
- $newArray = array();
- while(list($key,$val) = each($data)){
- $j = strlen($val);
- if($j < 4){
- /*If the string is not enough 4 digits, we fill it with 0*/
- $val = str_pad($val, 4, "0", STR_PAD_RIGHT);
- }
- for($ i=0;$i<$j;$i++){
- /*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" */
- if($val{$i} == 0){
- if($val{$i-1}){
- $newArray[$key][] = ' zero';
- }
- }else{
- $newArray[$key][] = $this->chinaData[$val{$i}].$this->chinaDataInt[$i+1];
- }
- }
- }
- unset($data,$key,$val);
- /*In the above loop, we have already obtained the array converted into Chinese; let me arrange it below*/
- foreach(array_reverse($newArray,true) as $key=>$val){
- if($key == 0){
- $str .= implode('',array_reverse($val));
- }
- if($key%2 == 1){
- $j = floor($key/2);
- if($j == 0){
- $str .= implode('',array_reverse($val)).'万';
- }else{
- $str .= implode('',array_reverse($val)).'万'.str_pad('',3*$j,'Billion');
- }
- }
- if($key%2 == 0 && $key != 0){
- if($key/2 > 1){
- $str .= implode('',array_reverse($val)).'万万'.str_pad('',3*(floor($ key/2)-1),'Billion');
- }else{
- $str .= implode('',array_reverse($val)).'Billion';
- }
- }
- }
- unset($newArray, $key,$val,$j);
- return $str;
- }
- //Tightly supports two decimal places
- private function setTransFloat($floatData,$pos){
- if($pos > 2){
- return $ this->errorNotice('def');
- }
- if($floatData{0} == 0){
- $data[] = 'zero';
- }else{
- $data[] = $this-> ;chinaData[$floatData{0}].$this->chinaDataFloat[1];
- }
- if($floatData!= 0 ){
- $data[] = $this->chinaData[$floatData In a flash].$this->chinaDataFloat[2];
- }
- return implode('',$data);
- }
- public function errorNotice($error){
- return $this->error[$error ];}}
- }
- $ num = New Transformnumbernew ('450252352007760006601000300', '80'); ; gettransfloat ();
-
-
- Copy code
|