Home  >  Article  >  Backend Development  >  Convert numerical amounts to Chinese uppercase characters in PHP_PHP Tutorial

Convert numerical amounts to Chinese uppercase characters in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:22:04853browse


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);


while ($j $m = substr($c,$j,4);
if ($m==zero yuan|| $m==zero million|| $m==zero million|| $m==zero zero){
$left=substr($c,0,$j);
$right=substr($c,$j+2);
$c = $left .$right;
$j = $j-2;
$slen = $slen-2;
}
$j=$j+2;
}
if( substr($c,strlen($c)-2,2)==zero){
$c=substr($c,0,strlen($c)-2);
} // if there is a 0 on the end , chop it out
return $c."whole";
}// end of function
?>

$out=num2rmb(1001.4570) ;
echo $out;
?>
If you have any good opinions, please contact me (cyman20@sina.com).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532361.htmlTechArticleIn 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 nothing...
Statement:
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