栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

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

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

moneyChangeBig(880070066.135 , true);

Class MoneyUnit{
    private $number;
    private $unit;

    
    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;
    }

    
    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;
    }

    
    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 => '兆',
        ];
    }
}

执行结果:

经过自己的基本测试没问题,如有大神发现问题,请及时评论告知,谢谢!!!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268820.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号