Home  >  Article  >  Backend Development  >  PHP function code for converting RMB amount numbers to Chinese capital_PHP tutorial

PHP function code for converting RMB amount numbers to Chinese capital_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:13:151281browse

I saw a very interesting PHP function on the Internet that converts RMB amount numbers into Chinese uppercase letters. The essence is to convert numbers into Chinese uppercase letters. I tested it and it was very interesting. Just input a number and you can print it out in uppercase letters. Novice friends, give it a try, draw inferences from one example, and write many interesting PHP conversion functions.

Copy code The code is as follows:

function cny($ns) {
static $ cnums=array("zero","one","two","three","four","五","鲁","旒","八","九"),
           = Array ("Yuan", "Corner", "Division"),
$ Grees = Array ("Pick", "Bai", "Thousand", "Wan", "Pick", "Bai", "Thousand", "Thousand", " ","billion");
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)));
return str_replace(array_keys($cnums),$cnums,$ret);
}




Copy code The code is as follows:

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);
}
return $ xs;
}
?>

Usage: Just pass the numeric parameters directly. The following example prints the numbers in capital letters. Output: One thousand two thousand one hundred twenty one yuan
Copy code The code is as follows:

echo cny('12121');
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326571.htmlTechArticleI saw a very interesting PHP function on the Internet that converts RMB amount numbers into Chinese capital letters. The essence is to convert numbers into Chinese capitalization, I tested it, it’s very interesting, just enter a number...
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