php 阿拉伯数字转中文大写金额
程序员文章站
2022-04-22 15:57:54
...
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] != '0'? $unit[$i%4] : ''; if($i>1 and $str[$i]+$str[$i-1] == 0){ $out[$i] = ''; } if($i%4 == 0){ $out[$i] .= $unit[4+floor($i/4)]; } } } $retval = join('',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)." ");
上一篇: php获取网页上所有链接的方法