- class TransFormNumber{
- public $chinaData = array('1'=>'壹','2'=>'贰','3'=>'叁','4'=>'肆','5'=>'伍','6'=>'陆','7'=>'柒','8'=>'捌','9'=>'玖');
- public $chinaDataInt = array('1'=>'元','2'=>'拾','3'=>'佰','4'=>'仟','5'=>'万','6'=>'拾','7'=>'佰','8'=>'仟','9'=>'亿','10'=>'拾','11'=>'佰','12'=>'仟','13'=>'万');
- public $chinaDataFloat = array('1'=>'角','2'=>'分');
- private $Intnumber; // string
- private $Floatnumber; // string
- public $error = array('0'=>'零','def'=>'数据格式不支持');
-
- public function __construct($intnumber,$floatnumber){
- $this->Intnumber = $intnumber;
- $this->Floatnumber = $floatnumber;
- }
- /*13位整数*/
- 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,''),'零');
- 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[] = '零';
- }
- }else{
- $data[] = '万';
- continue;
- }
- continue;
- }
- if($i%8 == 0 && $i != 0 && $oneData == 0){
- $data[] = '亿';
- }
- if($oneData == 0){
- if(($i == 4 || $i == 7)){
- if((substr($intData,$i-1,1) ==0)){
- continue;
- }else{
- $data[] = '零';
- }
- }else{
- if(substr($intData,$i-1,1) !=0 && $i !=3 && $i != 8){
- $data[] = '零';
- }else{
- if($i == 3 && substr($intData,$i,1) == 0 && substr($intData,$i-1,1) != 0){
- $data[] = '零';
- }
- }
- }
- }else{
- $data[] = $this->chinaData[$oneData].$this->chinaDataInt[$i+1];
- }
-
- }
- unset($intData,$oneData,$i);
- return $data;
- }
- //紧支持两位小数
- private function setTransFloat($floatData,$pos){
- if($pos > 2){
- return $this->errorNotice('def');
- }
- if($floatData{0} == 0){
- $data[] = '零';
- }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();
复制代码
|