数学数字转中文读法(1000000以内)
1 public function numToWrite($num){ 2 if($num>1000000){ 3 return '输入的数字不符合要求'; 4 } 5 $writeList=['零','一','二','三','四','五','六','七','八','九']; 6 $unitList=['十','百','千','万']; 7 $writeDec=''; 8 if(!is_int($num)){ 9 $writeDec='点'; 10 $decimal=explode('.',(string)$num)[1]; 11 if($decimal){ 12 for($i=0;$i<strlen($decimal);$i++){ 13 $writeDec.=$writeList[$decimal{$i}]; 14 } 15 } 16 } 17 $numInt=intval($num); 18 $i=1; 19 $writeStr=''; 20 while($numInt>=1){ 21 if($i==1){ 22 $writeStr=$writeList[$numInt-intval($numInt/10)*10].$writeStr; 23 }else{ 24 $j=$i-1; 25 $index=$j>count($unitList)?$j%count($unitList)-1:$j-1; 26 $writeIndex=$writeList[$numInt-intval($numInt/10)*10]; 27 $unit=$unitList[$index]!='万'&&$writeIndex=='零'?'':$unitList[$index]; 28 $writeIndex=$writeIndex=='零'?'':$writeIndex; 29 $writeIndex=$writeIndex.$unit; 30 $writeIndex=$writeIndex==''&&mb_substr($writeStr,0,1,'utf-8')!='零'?'零':$writeIndex; 31 32 $writeStr=$writeIndex.$writeStr; 33 } 34 $i++; 35 $numInt=intval($numInt/10); 36 } 37 $writeStr=mb_substr($writeStr,0,1,'utf-8')!='一'?$writeStr:mb_substr($writeStr,1,mb_strlen($writeStr,'utf-8')-1,'utf-8'); 38 $writeStr=mb_substr($writeStr,-1,1,'utf-8')!='零'?$writeStr:mb_substr($writeStr,0,mb_strlen($writeStr,'utf-8')-1,'utf-8'); 39 return $writeStr.$writeDec; 40 }