PHP将阿拉伯小写数字转为大写中文

PHP将阿拉伯小写数组转为大写中文

function numToWord($num)
        {
            $chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
            $chiUni = array('','十', '百', '千', '万', '亿', '十', '百', '千');

            $chiStr = '';

            $num_str = (string)$num;

            $count = strlen($num_str);
            $last_flag = true; //上一个 是否为0
            $zero_flag = true; //是否第一个
            $temp_num = null; //临时数字

            $chiStr = '';//拼接结果
            if ($count == 2) {//两位数
                $temp_num = $num_str[0];
                $chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
//当以1开头 都是十一,十二,以十开头的 我们就取$chiUni[i]也就是十当不是以1开头时,而是以2,3,4,我们取这个数字相应的中文并拼接上十
        $temp_num = $num_str[1];
        $chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
//取得第二个值并的到他的中文
    }else if($count > 2){
                $index = 0;
                for ($i=$count-1; $i >= 0 ; $i--) {
                    $temp_num = $num_str[$i];         //获取的个位数
                    if ($temp_num == 0) {
                        if (!$zero_flag && !$last_flag ) {
                            $chiStr = $chiNum[$temp_num]. $chiStr;
                            $last_flag = true;
                        }
                    }else{
                        $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;
//$index%9 index原始值为0,所以开头为0 后面根据循环得到:0,1,2,3...(不知道为什么直接用$index而是选择$index%9  毕竟两者结果是一样的)
//当输入的值为:1003 ,防止输出一千零零三的错误情况,$last_flag就起到作用了当翻译倒数第二个值时,将$last_flag设定为true;翻译第三值时在if(!$zero&&!$last_flag)的判断中会将其拦截,从而跳过
                        $zero_flag = false;
                        $last_flag = false;
                    }
                    $index ++;
                }
            }else{
                $chiStr = $chiNum[$num_str[0]];    //单个数字的7a64e58685e5aeb931333431336230直接取中文
            }
            return $chiStr;
        }

调用并传入要转入的数字即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值