- class TransFormNumber{
- public $chinaData = array('1'=>'壹','2'=>'二','3'=>'三', '4'=>'Si','5'=>'Wu','6'=>'Lu','7'=>'Qi','8'=>'Eight', '9'=>'玖');
- public $chinaDataInt = array('1'=>'Yuan','2'=>'Shi','3'=>'Hundred','4 '=>'Qian','5'=>'Ten thousand','6'=>'Shi','7'=>'Hundred','8'=>'Qian','9 '=>'Billion','10'=>'Shi','11'=>'Hundred','12'=>'Qian','13'=>'Ten thousand');
- 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;
- }
- /*13-digit integer*/
- public function getTransInt(){
- $this->Intnumber = intval($this->Intnumber);
- $len = strlen( $this->Intnumber);
- if($len > 13){
- return $this->errorNotice(1);
- }
- if($this->Intnumber == 0){
- return $this ->errorNotice('def');
- }
- $intData = strrev(substr($this->Intnumber,0,$len));
- $data = $this->setTransInt($intData,$len );
- krsort($data);
- $str = rtrim(join($data,''),'zero');
- if(substr($this->Intnumber,$len-1,1) == 0){
- $str.='元';
- }
- unset($data);
- return $str;
- }
- public function getTransFloat(){
- return $this->setTransFloat($this->Floatnumber ,strlen($this->Floatnumber));
- }
- private function setTransInt($intData,$pos){
- for($i=0;$i<$pos;$i++){
- $oneData = substr( $intData,$i,1);
- if(($i == 4 || ($i%8) == 4) && $oneData == 0){
- if(substr($intData,$i+1 ,1) == 0 && substr($intData,$i+2,1) == 0 && substr($intData,$i+3,1) == 0){
- if(substr($intData,$i -1,1) == 0){
- continue;
- }else{
- $data[] = 'zero';
- }
- }else{
- $data[] = '10,000';
- continue;
- }
- continue ;
- }
- if($i%8 == 0 && $i != 0 && $oneData == 0){
- $data[] = '100 million';
- }
- if($oneData == 0){
- if(($i == 4 || $i == 7)){
- if((substr($intData,$i-1,1) ==0)){
- continue;
- }else{
- $data [] = 'zero';
- }
- }else{
- if(substr($intData,$i-1,1) !=0 && $i !=3 && $i != 8){
- $data[] = 'zero';
- }else{
- if($i == 3 && substr($intData,$i,1) == 0 && substr($intData,$i-1,1) != 0){
- $data[] = 'zero';
- }
- }
- }
- }else{
- $data[] = $this->chinaData[$oneData].$this->chinaDataInt[$i+1];
- }
-
- }
- unset($intData,$oneData,$i);
- return $data;
- }
- //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($floatDataBB媪!= 0 ){
- $data[] = $this-> ;chinaData[$floatDataBB媪].$this->chinaDataFloat[2];
- }
- return implode('',$data);
- }
- public function errorNotice($error){
- return $this->error [$error];
- }
- }
-
- $num = new TransFormNumber('100228030','80');
- echo $num->getTransInt();
- echo $num->getTransFloat();
Copy code
|