Home >php教程 >PHP源码 >php 阿拉伯数字转中文大写金额

php 阿拉伯数字转中文大写金额

PHP中文网
PHP中文网Original
2016-05-23 16:38:471082browse

php代码

// 阿拉伯数字转中文大写金额
function NumToCNMoney($num,$mode = true,$sim = true){
	if(!is_numeric($num)) return '含有非数字非小数点字符!';
	$char    = $sim ? array('零','一','二','三','四','五','六','七','八','九')
	: array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
	$unit    = $sim ? array('','十','百','千','','万','亿','兆')
	: array('','拾','佰','仟','','萬','億','兆');
	$retval  = $mode ? '元':'点';
	//小数部分
	if(strpos($num, '.')){
		list($num,$dec) = explode('.', $num);
		$dec = strval(round($dec,2));
		if($mode){
			$retval .= "{$char[$dec['0']]}角{$char[$dec['1']]}分";
		}else{
			for($i = 0,$c = strlen($dec);$i < $c;$i++) {
				$retval .= $char[$dec[$i]];
			}
		}
	}
	//整数部分
	$str = $mode ? strrev(intval($num)) : strrev($num);
	for($i = 0,$c = strlen($str);$i < $c;$i++) {
		$out[$i] = $char[$str[$i]];
		if($mode){
			$out[$i] .= $str[$i] != &#39;0&#39;? $unit[$i%4] : &#39;&#39;;
				if($i>1 and $str[$i]+$str[$i-1] == 0){
				$out[$i] = &#39;&#39;;
			}
				if($i%4 == 0){
				$out[$i] .= $unit[4+floor($i/4)];
			}
		}
	}
	$retval = join(&#39;&#39;,array_reverse($out)) . $retval;
	return $retval;
}


echo (NumToCNMoney(2.55)."
");
echo (NumToCNMoney(2.55,1,0)."
");
echo (NumToCNMoney(7965)."
");
echo (NumToCNMoney(7965,1,0)."
");
echo (NumToCNMoney(155555555.68)."
");
echo (NumToCNMoney(155555555.68,1,0)."
");
echo (NumToCNMoney(0.8888888)."
");
echo (NumToCNMoney(0.8888888,1,0)."
");
echo (NumToCNMoney(99999999999)."
");
echo (NumToCNMoney(99999999999,1,0)."
");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn