Home  >  Article  >  Backend Development  >  PHP RMB lowercase to uppercase

PHP RMB lowercase to uppercase

WBOY
WBOYOriginal
2016-07-23 08:54:591157browse
[PHP] Code
  1. /**
  2. * Convert RMB from lowercase to uppercase
  3. *
  4. * @param string $number Value
  5. * @param string $int_unit Currency unit, default is "yuan", some requirements may be "round"
  6. * @param bool $is_round Whether to decimal Rounding
  7. * @param bool $is_extra_zero Whether to end the integer part with 0 and add 0 to the decimal number, such as 1960.30,
  8. * 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
  9. * @return string
  10. */
  11. public static function num2rmb($number = 0, $int_unit = '元', $is_round = TRUE, $is_extra_zero = FALSE) {
  12. // Will Divide the number into two pieces
  13. $parts = explode('.', $number, 2);
  14. $int = isset($parts[0]) ? strval($parts[0]) : '0';
  15. $dec = isset($parts[1]) ? strval($parts[1]) : '';
  16. // If there are more than 2 digits after the decimal point, just truncate without rounding, otherwise process
  17. $dec_len = strlen($ dec);
  18. if (isset($parts[1]) && $dec_len > 2) {
  19. $dec = $is_round
  20. ? substr(strrchr(strval(round(floatval("0.".$dec), 2 )), '.'), 1)
  21. : substr($parts[1], 0, 2);
  22. }
  23. // When number is 0.001, the amount after the decimal point is 0 yuan
  24. if (empty($ int) && empty($dec)) {
  25. return 'zero';
  26. }
  27. // Definition
  28. $chs = array('0','壹','二','三','四',' Wu','Lu','旒','捌','玖');
  29. $uni = array('','十','hundred','qian');
  30. $dec_uni = array('角', 'fen');
  31. $exp = array('', '万');
  32. $res = '';
  33. // Find the integer part from right to left
  34. for ($i = strlen($int) - 1, $k = 0; $i >= 0; $k++) {
  35. $str = '';
  36. // According to Chinese reading and writing habits, every 4 characters are converted into a paragraph, and i is constantly decreasing
  37. for ($j = 0; $j < 4 && $i >= 0; $j++, $i--) {
  38. $u = $int{$i} > 0 ? $uni[$j] : ' '; //Add unit
  39. after non-0 numbers $str = $chs[$int{$i}] . $u . $str;
  40. }
  41. //echo $str."|".($k - 2 )."
    ";
  42. $str = rtrim($str, '0');// Remove the trailing 0
  43. $str = preg_replace("/0+/", "zero", $str); // Replace multiple consecutive 0's
  44. if (!isset($exp[$k])) {
  45. $exp[$k] = $exp[$k - 2] . 'Billion'; // Building unit
  46. }
  47. $u2 = $str != '' ? $exp[$k] : '';
  48. $res = $str . $u2 . $res;
  49. }
  50. // If the decimal part is 00 after processing, it is required Process
  51. $dec = rtrim($dec, '0');
  52. // Find the decimal part from left to right
  53. if (!empty($dec)) {
  54. $res .= $int_unit;
  55. // Whether to append 0 to the number ending in 0 in the integer part. Some systems have this requirement
  56. if ($is_extra_zero) {
  57. if (substr($int, -1) === '0') {
  58. $res. = 'zero';
  59. }
  60. }
  61. for ($i = 0, $cnt = strlen($dec); $i < $cnt; $i++) {
  62. $u = $dec{$i} > 0 ? $dec_uni[$i] : ''; // Add unit after non-0 number
  63. $res .= $chs[$dec{$i}] . $u;
  64. }
  65. $res = rtrim($res , '0'); // Remove the trailing 0
  66. $res = preg_replace("/0+/", "zero", $res); // Replace multiple consecutive 0's
  67. } else {
  68. $res .= $int_unit . 'whole';
  69. }
  70. return $number < 0 ? "(negative)".$res : $res;
  71. }
Copy code
[PHP] Code
  1. echo num2rmb('123.45','round');one hundred twenty three round four corners five points
Copy code
PHP


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