首頁  >  文章  >  後端開發  >  php將人民幣中金額數字轉換為中文大寫代碼

php將人民幣中金額數字轉換為中文大寫代碼

伊谢尔伦
伊谢尔伦原創
2016-12-02 11:07:041221瀏覽

php將人民幣中金額數字轉換為中文大寫代碼

主體代碼:

function toChineseNumber($money){
    $money = round($money,2);
    $cnynums = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); 
    $cnyunits = array("圆","角","分");
    $cnygrees = array("拾","佰","仟","万","拾","佰","仟","亿"); 
    list($int,$dec) = explode(".",$money,2);
    $dec = array_filter(array($dec[1],$dec[0])); 
    $ret = array_merge($dec,array(implode("",$this->cnyMapUnit(str_split($int),$cnygrees)),"")); 
    $ret = implode("",array_reverse($this->cnyMapUnit($ret,$cnyunits))); 
    return str_replace(array_keys($cnynums),$cnynums,$ret); 
}
function cnyMapUnit($list,$units) { 
    $ul=count($units); 
    $xs=array(); 
    foreach (array_reverse($list) as $x) { 
        $l=count($xs); 
        if ($x!="0" || !($l%4)) 
            $n=($x=='0'?'':$x).($units[($l-1)%$ul]); 
        else $n=is_numeric($xs[0][0])?$x:''; 
 array_unshift($xs,$n); 
 } 
 return $xs; 
 }

呼叫代碼:

echo toChineseNumber($money);


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn