接收的项目有个需求要将小写金额改成大写,上个人也是在网上找的一个函数,有点小bug,我自己改了下,万亿以下应该不会有问题
function cny($ns) { $ns = str_replace(",","",$ns); static $cnums=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"), $cnyunits=array("圆","角","分"), $grees=array("拾","佰","仟","万","拾","佰","仟","亿"); list($ns1,$ns2)=explode(".",$ns,2); $ns2=array_filter(array($ns2[1],$ns2[0])); $ret=array_merge($ns2,array(implode("",_cny_map_unit(str_split($ns1),$grees)),"")); $ret=implode("",array_reverse(_cny_map_unit($ret,$cnyunits))); $str = str_replace(array_keys($cnums),$cnums,$ret); if(strpos($ns,".00")) { $str = $str."整"; } return $str;}function _cny_map_unit($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); } $xs= preg_replace('/0{2,}/', '0', str_replace('亿万', '亿', $xs));//出现亿万替换为亿,连续出现多个0时,合并一个 return $xs;}
主要是上面两个函数,用的时候直接使用
echo cny('200000000');