php数字转中文

function number2Chinese($num, $m = 1) {
	switch($m) {
		case 0:
			$CNum = array(
				array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'),
				array('','拾','佰','仟'),
				array('','萬','億','萬億')
			);
		break;
		default:
			$CNum = array(
				array('零','一','二','三','四','五','六','七','八','九'),
				array('','十','百','千'),
				array('','万','亿','万亿')
			);
		break;
	}

	if (!is_numeric($num)) {
		return false;
	}

	$flt = '';
	if (is_integer($num)) {
		$num = strval($num);
	}else if(is_numeric($num)){
		$num = strval($num);
		$rs = explode('.',$num,2);
		$num = $rs[0];
		$flt = $rs[1];
	}

	$len = strlen($num);
	$num = strrev($num);
	$chinese = '';
	
	for($i = 0,$k=0;$i < $len; $i+=4,$k++){
		$tmp_str = '';
		$str = strrev(substr($num , $i,4));
		$str = str_pad($str,4,'0',STR_PAD_LEFT);
		for ($j = 0; $j < 4; $j++) { 
			if($str{$j} !== '0'){
				$tmp_str .= $CNum[0][$str{$j}] . $CNum[1][4-1-$j];
			}
		}
		$tmp_str .= $CNum[2][$k];
		$chinese = $tmp_str . $chinese;
		unset($str);
	}
	if($flt !== ''){
		$str = '';
		for ($i=0; $i < strlen($flt); $i++) { 
			$str .= $CNum[0][$flt{$i}];
		}
		$chinese .= "点{$str}";
	}
	return $chinese;
}

$str = number2Chinese(123458656.789);
var_dump($str);

运行结果

string(63) "一亿二千三百四十五万八千六百五十六点七八九"
posted @ 2016-08-06 17:48  一叶(foolishnoob)  阅读(1110)  评论(0编辑  收藏  举报