This article has shared the implementation code of php Arabic numerals to Chinese RMB capital for your reference. The specific code is as follows
Code 1: PHP Arabic numerals to Chinese RMB capital, with detailed comments
/** * Function of converting numeric amounts into Chinese capital amounts *String Int $num Lowercase number or lowercase string to convert *return capital letters *The decimal digit is two digits **/ function num_to_rmb($num){ $c1 = "Zero One, Two Three, Four, Five, Four, and Four, and Four, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight, and Eight; $c2 = "Split the 100 million yuan"; //You don't need to be accurate after the segment, so you only have two decimal places left $num = round($num, 2); //Convert numbers into integers $num = $num * 100; if (strlen($num) > 10) { return "The amount is too large, please check it"; } $i = 0; $c = ""; while (1) { if ($i == 0) { //Get the last digit $n = substr($num, strlen($num)-1, 1); } else { $n = $num % 10; } //Convert the last digit into Chinese every time $p1 = substr($c1, 3 * $n, 3); $p2 = substr($c2, 3 * $i, 3); if ($n != '0' || ($n == '0' && ($p2 == '100 million' || $p2 == 'Ten thousand' || $p2 == 'Yuan'))) { $c = $p1 . $p2 . $c; } else { $c = $p1 . $c; } $i = $i + 1; //Remove the last digit $num = $num / 10; $num = (int)$num; //End the loop if ($num == 0) { break; } } $j = 0; $slen = strlen($c); while ($j < $slen) { //Utf8 One Chinese character is equivalent to 3 characters $m = substr($c, $j, 6); //Troubleshoot many 0s in numbers, remove one Chinese character "zero" in each loop if ($m == 'Zero Yuan' || $m == 'Zero Ten Thousand' || $m == 'Zero Billion' || $m == 'Zero Zero') { $left = substr($c, 0, $j); $right = substr($c, $j + 3); $c = $left . $right; $j = $j-3; $slen = $slen-3; } $j = $j + 3; } //This is to remove the last word "zero" similar to 23.0 if (substr($c, strlen($c)-3, 3) == 'zero') { $c = substr($c, 0, strlen($c)-3); } //Add the processed Chinese characters "Complete" if (empty($c)) { return "Zero Yuan"; }else{ return $c . "all"; } } echo num_to_rmb(23000000.00); //230,000 yuan
Code 2: PHP Arabic numerals to Chinese capital amount
// Arabic numerals to Chinese capital amountfunction NumToCNMoney($num,$mode = true,$sim = true){ if(!is_numeric($num)) return 'Contains non-digit non-decimal characters! '; $char = $sim ? array('zero','one','two','three','Four','five','six','seven','eight','Nine') : array('zero','one','two','3','Si','Wu','land','Qi','eight','Nine'); $unit = $sim ? array('','ten','Hundred','thousand','','Ten thousand','100 million','mega') : array('','pickup','Bai','thousand','','ten thousand','All','mega'); $retval = $mode ? 'Yuan':'point'; //Decimal Part if(strpos($num, '.')){ list($num,$dec) = explode('.', $num); $dec = strval(round($dec,2)); if($mode){ $retval .= "{$char[$dec['0']]}horn{$char[$dec['1']]}point"; }else{ for($i = 0,$c = strlen($dec);$i < $c;$i++) { $retval .= $char[$dec[$i]]; } } } //Integer part $str = $mode ? strrev(intval($num)) : strrev($num); for($i = 0,$c = strlen($str);$i < $c;$i++) { $out[$i] = $char[$str[$i]]; if($mode){ $out[$i] .= $str[$i] != '0'? $unit[$i%4] : ''; if($i>1 and $str[$i]+$str[$i-1] == 0){ $out[$i] = ''; } if($i%4 == 0){ $out[$i] .= $unit[4+floor($i/4)]; } } } $retval = join('',array_reverse($out)) . $retval; return $retval; } echo (NumToCNMoney(2.55)."<br>"); echo (NumToCNMoney(2.55,1,0)."<br>"); echo (NumToCNMoney(7965)."<br>"); echo (NumToCNMoney(7965,1,0)."<br>"); echo (NumToCNMoney(155555555.68)."<br>"); echo (NumToCNMoney(155555555.68,1,0)."<br>"); echo (NumToCNMoney(0.8888888)."<br>"); echo (NumToCNMoney(0.8888888,1,0)."<br>"); echo (NumToCNMoney(99999999999)."<br>"); echo (NumToCNMoney(99999999999,1,0)."<br>");
I hope this article will be helpful for everyone to learn PHP programming.