[PHP] Code
- /**
- * Convert RMB from lowercase to uppercase
- *
- * @param string $number Value
- * @param string $int_unit Currency unit, default is "yuan", some requirements may be "round"
- * @param bool $is_round Whether to decimal Rounding
- * @param bool $is_extra_zero Whether to end the integer part with 0 and add 0 to the decimal number, such as 1960.30,
- * Some systems require the output of "One thousand nine hundred six hundred yuan zero three cents", in fact" "One thousand, nine hundred, ten yuan and three cents" is also correct
- * @return string
- */
- public static function num2rmb($number = 0, $int_unit = '元', $is_round = TRUE, $is_extra_zero = FALSE) {
-
- // Will Divide the number into two pieces
- $parts = explode('.', $number, 2);
- $int = isset($parts[0]) ? strval($parts[0]) : '0';
- $dec = isset($parts[1]) ? strval($parts[1]) : '';
-
- // If there are more than 2 digits after the decimal point, just truncate without rounding, otherwise process
- $dec_len = strlen($ dec);
- if (isset($parts[1]) && $dec_len > 2) {
- $dec = $is_round
- ? substr(strrchr(strval(round(floatval("0.".$dec), 2 )), '.'), 1)
- : substr($parts[1], 0, 2);
- }
-
- // When number is 0.001, the amount after the decimal point is 0 yuan
- if (empty($ int) && empty($dec)) {
- return 'zero';
- }
-
- // Definition
- $chs = array('0','壹','二','三','四',' Wu','Lu','旒','捌','玖');
- $uni = array('','十','hundred','qian');
- $dec_uni = array('角', 'fen');
- $exp = array('', '万');
- $res = '';
-
- // Find the integer part from right to left
- for ($i = strlen($int) - 1, $k = 0; $i >= 0; $k++) {
- $str = '';
- // According to Chinese reading and writing habits, every 4 characters are converted into a paragraph, and i is constantly decreasing
- for ($j = 0; $j < 4 && $i >= 0; $j++, $i--) {
- $u = $int{$i} > 0 ? $uni[$j] : ' '; //Add unit
- after non-0 numbers $str = $chs[$int{$i}] . $u . $str;
- }
- //echo $str."|".($k - 2 )."
";
- $str = rtrim($str, '0');// Remove the trailing 0
- $str = preg_replace("/0+/", "zero", $str); // Replace multiple consecutive 0's
- if (!isset($exp[$k])) {
- $exp[$k] = $exp[$k - 2] . 'Billion'; // Building unit
- }
- $u2 = $str != '' ? $exp[$k] : '';
- $res = $str . $u2 . $res;
- }
-
- // If the decimal part is 00 after processing, it is required Process
- $dec = rtrim($dec, '0');
-
- // Find the decimal part from left to right
- if (!empty($dec)) {
- $res .= $int_unit;
-
- // Whether to append 0 to the number ending in 0 in the integer part. Some systems have this requirement
- if ($is_extra_zero) {
- if (substr($int, -1) === '0') {
- $res. = 'zero';
- }
- }
-
- for ($i = 0, $cnt = strlen($dec); $i < $cnt; $i++) {
- $u = $dec{$i} > 0 ? $dec_uni[$i] : ''; // Add unit after non-0 number
- $res .= $chs[$dec{$i}] . $u;
- }
- $res = rtrim($res , '0'); // Remove the trailing 0
- $res = preg_replace("/0+/", "zero", $res); // Replace multiple consecutive 0's
- } else {
- $res .= $int_unit . 'whole';
- }
- return $number < 0 ? "(negative)".$res : $res;
- }
Copy code
[PHP] Code
- echo num2rmb('123.45','round');one hundred twenty three round four corners five points
Copy code
|