当前位置:首页 > PHP教程 >

php数字转大小写

发布时间:2024-01-18 12:25:37 作者:佚名 阅读:(11)

最近一个项目中用到了需要把阿拉伯数字转换成汉字大小写,接下来云梦编程为大家介绍一下php数字转大小写的方法,如果你也遇到了相同的需求,可以参考本文中的方法

php数字转大小写

1、处理思路:

首先判断输出的金额是否为数字或数字字符串;然后预定义中文转换的数组以及单位转换的数组;接着将金额的数值字符串拆分成数组;最后将整数部分替换成大写汉字即可。

2、函数:

/*
 * php阿拉伯数字转化为汉字大小写
 * @param $num 数字
 * @param $type 类型
 * @return array 返回数字
 */
function num2Chinese($num, $type = 1) {
    if (preg_match("/[\x7f-\xff]/", $num)) {
        return "数字中包含汉字,请修改!";
    }
    switch($type) {
        case 0:
            $CNum = array(
                array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'),
                array('','拾','佰','仟'),
                array('','萬','億','萬億')
            );
            break;
        default:
            $CNum = array(
                array('零','一','二','三','四','五','六','七','八','九'),
                array('','十','百','千'),
                array('','万','亿','万亿')
            );
            break;
    }
    if (is_integer($num)) {
        $int = (string)$num;
    } else if (is_numeric($num)) {
        $num = explode('.', (string)floatval($num));
        $int = $num[0];
        $fl  = isset($num[1]) ? $num[1] : FALSE;
    }
    // 长度
    $len = strlen($int);
    // 中文
    $chinese = array();
    // 反转的数字
    $str = strrev($int);
    for($i = 0; $i<$len; $i+=4 ) {
        $s = array(0=>$str[$i], 1=>@$str[$i+1], 2=>@$str[$i+2], 3=>@$str[$i+3]);
        $j = '';
        // 千位
        if ($s[3] !== '') {
            $s[3] = (int) $s[3];
            if ($s[3] !== 0) {
                $j .= $CNum[0][$s[3]].$CNum[1][3];
            } else {
                if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0) {
                    $j .= $CNum[0][0];
                }
            }
        }
        // 百位
        if ($s[2] !== '') {
            $s[2] = (int) $s[2];
            if ($s[2] !== 0) {
                $j .= $CNum[0][$s[2]].$CNum[1][2];
            } else {
                if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
                    $j .= $CNum[0][0];
                }
            }
        }
        // 十位
        if ($s[1] !== '') {
            $s[1] = (int) $s[1];
            if ($s[1] !== 0) {
                $j .= $CNum[0][$s[1]].$CNum[1][1];
            } else {
                if ($s[0]!=0 && $s[2] != 0) {
                    $j .= $CNum[0][$s[1]];
                }
            }
        }
        // 个位
        if ($s[0] !== '') {
            $s[0] = (int) $s[0];
            if ($s[0] !== 0) {
                $j .= $CNum[0][$s[0]].$CNum[1][0];
            } else {
                // $j .= $CNum[0][0];
            }
        }
        $j.=$CNum[2][$i/4];
        array_unshift($chinese, $j);
    }
    $chs = implode('', $chinese);
    if (@$fl) {
        $chs .= '点';
        for($i=0,$j=strlen($fl); $i<$j; $i++) {
            $t = (int)$fl[$i];
            $chs.= $str[0][$t];
        }
    }
    return $chs;
}

3、函数调用

$nums = num2Chinese('1230');
var_dump($nums);


以上就是云梦编程为大家介绍的关于php阿拉伯数字转换成汉字大小写方法的全部内容了,了解更多相关文章请关注云梦编程网!

© 2023 - 云梦编程网 版权所有 鲁ICP备2021017318号-4