阿拉伯数字转化为中文汉字(大、小写) - PHP

代码

function number2Chinese($num, $m = 1) {
    switch($m) {
        case 0:
            $CNum = array(
                        array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'),
                        array('','拾','佰','仟'),
                        array('','萬','億','萬億')
                    );
            break;
        default:
            $CNum = array(
                    array('零','一','二','三','四','五','六','七','八','九'),
                    array('','十','百','千'),
                    array('','万','亿','万亿')
            );
            break;
    }
      
    // $cNum = array('零','一','二','三','四','五','六','七','八','九');
 
    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);
      
    // <a href="http://www.sharetk.com/" target="_blank"><u>中文</u></a>
    $chinese = array();
 
    // 反转的<a href="http://www.sharetk.com/" target="_blank"><u>数字</u></a>
    $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;
} 

 

源于:http://www.sharetk.com/html/code/php/5470.html

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值