最近老是遇到數字轉換中文的問題,寫了個分享一下。大家多指教。
- /*
- * func 數位轉換中文類別
- * Author shuang
- * date 2012-08-17
- * email:shuangbrmail:shuang.
- */
-
- class TransFormNumberNew{
- public $chinaData = array('1'=>'壹','2'=>'貳','3'=>'叁', '4'=>'肆','5'=>'伍','6'=>'陸','7'=>'柒','8'=>'捌','9'=> '玖');
- public $chinaDataInt = array('1'=>'','2'=>'拾取','3'=>'佰','4'=>'仟');
- 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;
- }
- public function getTransInt(){
- /*如果數字是0或非數字字元回傳錯誤提示* /
- if($this->Intnumber == 0){
- return $this->errorNotice('def');
- }
- if(!preg_match('/^d+$/' ,$this->Intnumber)){
- return $this->errorNotice('def');
- }
- /*移除字串開頭是0的字元*/
- $this-> dealIntZero();
-
- $data = array();
- /*把字串分成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 if($this->Intnumber{$i} != 0){
- $this->Intnumber = substr($this->Intnumber,$i,$j);
- break ;
- }
- }
- }
- private function setTransInt($data){
- $str = '';
- $newArray = array();
- while(list(list(> $newArray = array();
- while(list(list( $key,$val) = each($data)){
- $j = strlen($val);
- if($j /*如果字串不夠4位,我們用0補齊*/
- $val = str_pad($val, 4, "0", STR_PAD_RIGHT);
- }
- for($i=0;$i /*每四個字串一循環;如果字串為0,判斷一下它的前一位是否為0,如果是0,不處理。不是0,我們用「零」補齊*/
- if($val{$i} == 0){
- if($val{$i-1}){
- $newArray[$ key][] = '零';
- }
- }else{
- $newArray[$key][] = $this->chinaData[$val{$i}].$this->chinaDataInt [$i+1];
- }
- }
- }
- unset($data,$key,$val);
- /*上面的循環我們已經得到了轉換成中文的陣列;下面我排列即可*/
- 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,'億');
- }
- }
- if($key%2 == 0 && $key != 0){
- if($key/2 > 1){
- $str .= implode('',array_reverse($val)).'萬萬'.str_pad('',3*(floor($key/ 2)-1),'億');
- }else{
- $str .= implode('',array_reverse($val)).'億';
- }
- }
- }
- unset($newArray,$key,$val,$j);
- return $str;
- }
- //緊支援兩位小數
- 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($floatData一念之間!= 0 ){
- $data[] = $this->chinaData[$floatData一念之間].$this->chinaDataFloat[2];
- }
- return implode('',$data);
- }
- public function errorNotice($error){
- return $this->error[$error];
- }
- }
- $num = new TransFormNumberNew('45025235200776000601000300','80');
- echo $num->getTransInt();
echo $num->get 複製程式碼
|