PHP 万亿级别以下的int类型(钱)单位转为汉字

<?php
/**
 * Created by PhpStorm.
 * User: msi
 * Date: 2019/9/23
 * Time: 22:07
 */

//输入要转换的金额
$moneyUnitObj = new MoneyUnit();
echo $moneyUnitObj->moneyChangeBig(880070066.135 , true);

Class MoneyUnit{
    private $number;
    private $unit;

    /**
     * 金钱转大写
     * @param $money
     * @param bool $numerous
     * @return string
     */
    public function moneyChangeBig($money , $numerous = false)
    {
        //不得超过万亿
        if (strlen($money)>13){
            echo '你咋这么牛逼,你有这么多钱???';die;
        }
        if ($numerous){
            $this->numerousNumberAndUnit();
        }else{
            $this->janeNumberAndUnit();
        }
        mb_internal_encoding('UTF-8');

        //判断是不是负数
        $negativeNumber = '';
        $prefixNum = substr($money , 0 ,1);
        if ($prefixNum == '-'){
            $negativeNumber = '负';
            $money = substr($money , 1);
        }

        //判断有没有小数点
        if (strpos($money , '.')){
            $tmpMoney = explode('.' , $money);
            $result = $this->integerPart($tmpMoney[0]) . $this->decimalPart($tmpMoney[1]);
        }else{
            $result = $this->integerPart($money);
        }

        return $negativeNumber . $result;
    }

    /**
     * 计算整数部分
     * @param $money
     * @return string
     */
    private function integerPart($money){
        $result = '';
        $len = strlen($money);
        if ($len == 1) {
            $result = $this->number[$money] . $this->unit[1];
        } else {
            for ($i = 0; $i < $len; $i++) {
                $tmpNum = substr($money, $i, 1);
                $tmpUnit = strlen(substr($money, $i));
                if ($tmpNum == 0) {
                    if (mb_substr($result, -1) != $this->number[0]) {
                        if ($tmpUnit == 9 || $tmpUnit == 5){
                            $result .= $this->unit[$tmpUnit];
                        }else{
                            $result .= $this->number[0];
                        }
                    }
                } else {
                    $result .= $this->number[$tmpNum] . $this->unit[$tmpUnit];
                }
            }
        }
        $tmpStr = mb_substr($result, -1);
        if ($tmpStr == $this->number[0]) {
            $result = mb_substr($result, 0, -1) . $this->unit[1];
        } elseif ($tmpStr != $this->unit[1]) {
            $result .= $this->unit[1];
        }
        return $result;
    }

    /**
     * 计算小数部分
     * @param $money
     * @return string
     */
    private function decimalPart($money){
        if (empty($money)){
            echo '你是傻逼吗?点后面啥也不写??';die;
        }

        $result = $this->number[$money[0]] . '角';
        if (strlen($money) > 1){
            $result .= $this->number[$money[1]] . '分';
        }
        return $result;
    }

    //简体数字和单位
    private function janeNumberAndUnit(){
        $this->number = [
            0 => '零',
            1 => '一',
            2 => '二',
            3 => '三',
            4 => '四',
            5 => '五',
            6 => '六',
            7 => '七',
            8 => '八',
            9 => '九',
            10 => '十'
        ];
        $this->unit = [
            1 => '元',
            2 => '十',
            3 => '百',
            4 => '千',
            5 => '万',
            6 => '十',
            7 => '百',
            8 => '千',
            9 => '亿',
            10 => '十',
            11 => '百',
            12 => '千',
            13 => '万',
            14 => '兆'
        ];
    }
    //繁体数字和单位
    private function numerousNumberAndUnit(){
        $this->number = [
            0 => '零',
            1 => '壹',
            2 => '贰',
            3 => '叁',
            4 => '肆',
            5 => '伍',
            6 => '陆',
            7 => '柒',
            8 => '捌',
            9 => '玖',
            10 => '拾'
        ];
        $this->unit = [
            1 => '元',
            2 => '拾',
            3 => '佰',
            4 => '仟',
            5 => '万',
            6 => '拾',
            7 => '佰',
            8 => '仟',
            9 => '亿',
            10 => '拾',
            11 => '佰',
            12 => '仟',
            13 => '万',
            14 => '兆',
        ];
    }
}

执行结果:

经过自己的基本测试没问题,如有大神发现问题,请及时评论告知,谢谢!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值