博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

PHP金额大写

Posted on 2024-10-09 17:09  PHP-张工  阅读(24)  评论(0编辑  收藏  举报

在网上找了好几个金额大写的代码,经过测试都多少有点问题。代码根据网上的代码优化了以下内容:

1. 连续多个零时,只显示1个零
2. 超过1万时,并且万位无数字时,万不显示
3. 小数只有分时,会显示为角
4. 负数时添加负号

将金额转换为大写,代码如下:

    /**
     * 将数值金额转换为中文大写金额
     */
    public static function convertAmountToCn($amount) {
        $capitals = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
        $units = array('元', '角', '分', '元整');
        $amount = round($amount, 2);
        $integerPart = (int)$amount;
        $fractionalPart = round(($amount - $integerPart) * 100);
        $isNegative = false;
        if ($integerPart < 0){
            $isNegative = true;
            $integerPart = -$integerPart;
            $fractionalPart = -$fractionalPart;
        }

        $chineseInteger = '';
        if ($integerPart == 0) {
            $chineseInteger = '零';
        } else {
            $integerDigits = str_split($integerPart);
            $digitCount = count($integerDigits);
            if ($digitCount > 12){
                return "金额格式错误,整数部分最多12位";
            }
            for ($i = 0; $i < $digitCount; $i++) {
                $digit = $integerDigits[$i];
                $chineseInteger.= $capitals[$digit];
                if ($i < $digitCount - 1) {
                    if ($digit == 0) {
                        $chineseInteger = rtrim($chineseInteger, '零');
                        if ($i == ($digitCount - 5)) {
                            if (mb_substr($chineseInteger, -1) != '亿'){
                                $chineseInteger.= '万';
                            }
                        }else if ($i == ($digitCount - 9)) {
                            $chineseInteger.= '亿';
                        }else{
                            $chineseInteger.= '零';
                        }
                    } elseif ($i == ($digitCount - 2)) {
                        $chineseInteger.= '拾';
                    } elseif ($i == ($digitCount - 3)) {
                        $chineseInteger.= '佰';
                    } elseif ($i == ($digitCount - 4)) {
                        $chineseInteger.= '仟';
                    } elseif ($i == ($digitCount - 5)) {
                        $chineseInteger.= '万';
                    } elseif ($i == ($digitCount - 6)) {
                        $chineseInteger.= '拾';
                    } elseif ($i == ($digitCount - 7)) {
                        $chineseInteger.= '佰';
                    } elseif ($i == ($digitCount - 8)) {
                        $chineseInteger.= '仟';
                    } elseif ($i == ($digitCount - 9)) {
                        $chineseInteger.= '亿';
                    } elseif ($i == ($digitCount - 10)) {
                        $chineseInteger.= '拾';
                    } elseif ($i == ($digitCount - 11)) {
                        $chineseInteger.= '佰';
                    } elseif ($i == ($digitCount - 12)) {
                        $chineseInteger.= '仟';
                    }
                }
            }
        }

        if ($chineseInteger != '零') {
            $chineseInteger = rtrim($chineseInteger, '零');
        }

        $chineseFractional = '';
        if ($fractionalPart > 0) {
            $fractionalDigits = str_split((int) $fractionalPart);
            if ($fractionalPart < 10){
                array_unshift($fractionalDigits, 0);
            }
            $chineseFractional.= $capitals[$fractionalDigits[0]]. $units[1];
            if (isset($fractionalDigits[1]) && $fractionalDigits[1] > 0) {
                $chineseFractional.= $capitals[$fractionalDigits[1]]. $units[2];
            }
        }
        if ($chineseFractional == '') {
            $chineseCurrency = $chineseInteger. $units[3];
        } else {
            $chineseCurrency = $chineseInteger. $units[0]. $chineseFractional;
        }

        if ($isNegative) {
            $chineseCurrency = '负'. $chineseCurrency;
        }

        return $chineseCurrency;
    }

测试结果:

123456789 => 壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整
10000006 => 壹仟万零陆元整
102030405 => 壹亿零贰佰零叁万零肆佰零伍元整
123.36 => 壹佰贰拾叁元叁角陆分
1000152000 => 壹拾亿零壹拾伍万贰仟元整
800000000001 => 捌仟亿零壹元整
10000000 => 壹仟万元整
800001000000 => 捌仟亿零壹佰万元整
800010000000 => 捌仟亿壹仟万元整
800000000000.33 => 捌仟亿元叁角叁分
-123456789.66 => 负壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元陆角陆分
1.06 => 壹元零角陆分
0.06 => 零元零角陆分
0.1 => 零元壹角