In the process of developing PHP applications, I often encounter the task of converting numerical amounts into uppercase Chinese characters. I thought there must be a compiled PHP function on the Internet. But I searched and searched, but couldn't find it. I had no choice but to do it myself. Let’s take it out and share it with you now. I hope that we can change the history that cannot be found even if we look for it.
function num2rmb ($num){ file://Function that converts digital amounts into Chinese uppercase numbers
$c1="zero one two three four five land seven 捌玖";
$c2="One hundred million ten thousand cents";
$num=round($num,2);
$num=$num*100;
if(strlen( $num)>10){
return "oh, sorry, the number is too long!";
}
$i=0;
$c="";
while ( 1){
if($i==0){
$n=substr($num,strlen($num)-1,1);
}else{
$n=$ num %10;
}
$p1=substr($c1,2*$n,2);
$p2=substr($c2,2*$i,2);
if ($n!=0 || ($n==0 &&($p2==100 million|| $p2==10,000|| $p2==yuan))){
$c=$p1.$p2 .$c;
}else{
$c=$p1.$c;
}
$i=$i+1;
$num=$num/10;
$num=(int)$num;
if($num==0){
break;
}
}//end of while| here, we got a chinese string with some useless character
f//we chop out the useless characters to form the correct output
$j = 0;
$slen=strlen($c);