num_to_rmb(98765432.1);
/*
* Function to convert a numeric amount to a Chinese uppercase amount.
* String Int $num The lowercase number or lowercase string to be converted.
* return uppercase number
*/
public function num_to_rmb($num)
{
$c1 = "Zero, two, two, three, four, five, seven, eight, nine.";
$c2 = "Cent-dollar, ten million, ten thousand million, one hundred billion.";
//Precise to the minute followed by don't want, so only two decimal places are left
$num = round($num, 2);
// Convert numbers to integers
$num = $num * 100;//12312
if (strlen($num) > 10) {
return "The amount is too large. Please check.";
}
$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 to Chinese each time
$p1 = substr($c1, 3 * $n, 3);
$p2 = substr($c2, 3 * $i, 3);
if ($n != '0' || ($n == '0' && ($p2 == 'Billions' || $p2 == 'Wan' || $p2 == 'Yuan'))) {
$c = $p1 . $p2 . $c;
} else {
$c = $p1 . $c;
}
$i = $i + 1;
//Removed the last digit of the number.
$num = $num / 10;
$num = (int)$num;
//End the loop
if ($num == 0) {
break;
}
}
$j = 0;
$slen = strlen($c);
while ($j < $slen) {
//utf8 A Chinese character is equivalent to 3 characters.
$m = substr($c, $j, 6);
// Handle the case of a lot of zeros in a number by removing one Chinese character "zero" in each cycle.
if ($m == 'Zero dollars' || $m == 'Zero million' || $m == 'Zero billion' || $m == '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 "zero" like in 23.0.
if (substr($c, strlen($c)-3, 3) == 'Zero') {
$c = substr($c, 0, strlen($c)-3);
}
// Add "whole" to the processed characters.
if (empty($c)) {
return "Zero dollars and change.";
}else{
return $c . "Whole.";
}
}