Home > Backend Development > PHP Tutorial > PHP converts the amount of money in RMB into Chinese uppercase code

PHP converts the amount of money in RMB into Chinese uppercase code

伊谢尔伦
Release: 2023-03-02 21:08:01
Original
1241 people have browsed it

PHP converts the amount of money in RMB into Chinese uppercase code

Body code:

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); 
}
Copy after login
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; 
 }
Copy after login

Calling code:

echo toChineseNumber($money);
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template