数学数字转中文读法(1000000以内)

数学数字转中文读法(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     }

 

转载于:https://www.cnblogs.com/mudaoyuye/p/9573319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值