php下实现一个阿拉伯数字转中文数字的函数
如果要用于金额的转换,对小数部分的处理要做一下修改
<?php
function ch_num($num,$mode=true) {
$char = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
$dw = array("","拾","佰","仟","","f","|","兆");
$dec = "c";
$retval = "";
if($mode)
preg_match_all("/^0*(\d*)\.?(\d*)/",$num, $ar);
else
preg_match_all("/(\d*)\.?(\d*)/",$num, $ar);
if($ar[2][0] != "")
$retval = $dec . ch_num($ar[2][0],false); //如果有小数,先递归处理小数
if($ar[1][0] != "") {
$str = strrev($ar[1][0]);
for($i=0;$i<strlen($str);$i++) {
$out[$i] = $char[$str[$i]];
if($mode) {
$out[$i] .= $str[$i] != "0"? $dw[$i%4] : "";
if($str[$i]+$str[$i-1] == 0)
$out[$i] = "";
if($i%4 == 0)
$out[$i] .= $dw[4+floor($i/4)];
}
}
$retval = join("",array_reverse($out)) . $retval;
}
return $retval;
}
//echo ch_num("12345006789001.123");
//echo ch_num("880079.1234");
echo ch_num("300045.0123");
?>
<?php
function ch_num($num,$mode=true) {
$char = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
$dw = array("","拾","佰","仟","","f","|","兆");
$dec = "c";
$retval = "";
if($mode)
preg_match_all("/^0*(\d*)\.?(\d*)/",$num, $ar);
else
preg_match_all("/(\d*)\.?(\d*)/",$num, $ar);
if($ar[2][0] != "")
$retval = $dec . ch_num($ar[2][0],false); //如果有小数,先递归处理小数
if($ar[1][0] != "") {
$str = strrev($ar[1][0]);
for($i=0;$i<strlen($str);$i++) {
$out[$i] = $char[$str[$i]];
if($mode) {
$out[$i] .= $str[$i] != "0"? $dw[$i%4] : "";
if($str[$i]+$str[$i-1] == 0)
$out[$i] = "";
if($i%4 == 0)
$out[$i] .= $dw[4+floor($i/4)];
}
}
$retval = join("",array_reverse($out)) . $retval;
}
return $retval;
}
//echo ch_num("12345006789001.123");
//echo ch_num("880079.1234");
echo ch_num("300045.0123");
?>
相关推荐
Lenskit 2019-06-26
ltswind 2017-10-31
bryantmeng 2017-05-26
luoxinyurose 2019-04-21
aqua0 2009-08-21
Oraclezcy 2011-08-06
KUNPHP 2019-04-13
PHP100 2019-03-28
PHP100 2019-03-27
PHP100 2019-03-27
PHP100 2019-03-27
编程10000问 2019-03-26
VTEC 2018-01-17
BitTigerio 2018-01-12
编程爱好者联盟 2017-03-23