Home > Backend Development > PHP Tutorial > Convert Arabic numerals to Chinese characters (upper and lower case)

Convert Arabic numerals to Chinese characters (upper and lower case)

WBOY
Release: 2016-07-25 09:01:44
Original
1672 people have browsed it
To convert numbers into Chinese characters
I wrote one myself
  1. function number2Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. array('zero','壹','二','三','四','五','鲁','旒','捌','玖'),
  6. array('','十','百','千'),
  7. array(' ','million','billion','trillion')
  8. );
  9. break;
  10. default:
  11. $CNum = array(
  12. array('zero','one','two','three',' four','five','six','seven','eight','nine'),
  13. array('','ten','hundred','thousand'),
  14. array('',' Wan','billion','trillion')
  15. );
  16. break;
  17. }
  18. // $cNum = array('zero','one','two','three','four',' five','six','seven','eight','nine');
  19. if (is_integer($num)) {
  20. $int = (string)$num;
  21. } else if (is_numeric($num )) {
  22. $num = explode('.', (string)floatval($num));
  23. $int = $num[0];
  24. $fl = isset($num[1]) ? $num[1 ] : FALSE;
  25. }
  26. // Length
  27. $len = strlen($int);
  28. // Chinese
  29. $chinese = array();
  30. // Reverse number
  31. $str = strrev($int );
  32. for($i = 0; $i<$len; $i+=4 ) {
  33. $s = array(0=>$str[$i], 1=>$str[$i+ 1 ], 2=>$str[$i+2], 3=>$str[$i+3]);
  34. $j = '';
  35. // Thousands digit
  36. if ($s[3 ] !== '') {
  37. $s[3] = (int) $s[3];
  38. if ($s[3] !== 0) {
  39. $j .= $CNum[0][$ s [3]].$CNum[1][3];
  40. } else {
  41. if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0 ) {
  42. $j .= $CNum[0][0];
  43. }
  44. }
  45. }
  46. // Hundreds place
  47. if ($s[2] !== '') {
  48. $s[2] = ( int ) $s[2];
  49. if ($s[2] !== 0) {
  50. $j .= $CNum[0][$s[2]].$CNum[1][2];
  51. } else {
  52. if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
  53. $j .= $CNum[0][0] ;
  54. }
  55. }
  56. }
  57. // Tens digit
  58. if ($s[1] !== '') {
  59. $s[1] = (int) $s[1];
  60. if ($s[1 ] !== 0) {
  61. $j .= $CNum[0][$s[1]].$CNum[1][1];
  62. } else {
  63. if ($s[0]!=0 && $ s[2] != 0) {
  64. $j .= $CNum[0][$s[1]];
  65. }
  66. }
  67. }
  68. // ones digit
  69. if ($s[0] !== ' ') {
  70. $s[0] = (int) $s[0];
  71. if ($s[0] !== 0) {
  72. $j .= $CNum[0][$s[0] ] .$CNum[1][0];
  73. } else {
  74. // $j .= $CNum[0][0];
  75. }
  76. }
  77. $j.=$CNum[2][$ i /4];
  78. array_unshift($chinese, $j);
  79. }
  80. $chs = implode('', $chinese);
  81. if ($fl) {
  82. $chs .= 'point';
  83. for ( $i=0,$j=strlen($fl); $i<$j; $i++) {
  84. $t = (int)$fl[$i];
  85. $chs.= $str[0][ $ t];
  86. }
  87. }
  88. return $chs;
  89. }
Copy code
  1. function number2Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. array('zero','壹','二','三','四','五','鲁','旒','捌','玖'),
  6. array('','十','百','千'),
  7. array(' ','million','billion','trillion')
  8. );
  9. break;
  10. default:
  11. $CNum = array(
  12. array('zero','one','two','three',' four','five','six','seven','eight','nine'),
  13. array('','ten','hundred','thousand'),
  14. array('',' Wan','billion','trillion')
  15. );
  16. break;
  17. }
  18. // $cNum = array('zero','one','two','three','four',' five','six','seven','eight','nine');
  19. if (is_integer($num)) {
  20. $int = (string)$num;
  21. } else if (is_numeric($num )) {
  22. $num = explode('.', (string)floatval($num));
  23. $int = $num[0];
  24. $fl = isset($num[1]) ? $num[1 ] : FALSE;
  25. }
  26. // Length
  27. $len = strlen($int);
  28. // Chinese
  29. $chinese = array();
  30. // Reverse number
  31. $str = strrev($int );
  32. for($i = 0; $i<$len; $i+=4 ) {
  33. $s = array(0=>$str[$i], 1=>$str[$i+ 1 ], 2=>$str[$i+2], 3=>$str[$i+3]);
  34. $j = '';
  35. // Thousands digit
  36. if ($s[3 ] !== '') {
  37. $s[3] = (int) $s[3];
  38. if ($s[3] !== 0) {
  39. $j .= $CNum[0][$ s [3]].$CNum[1][3];
  40. } else {
  41. if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0 ) {
  42. $j .= $CNum[0][0];
  43. }
  44. }
  45. }
  46. // Hundreds place
  47. if ($s[2] !== '') {
  48. $s[2] = ( int ) $s[2];
  49. if ($s[2] !== 0) {
  50. $j .= $CNum[0][$s[2]].$CNum[1][2];
  51. } else {
  52. if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
  53. $j .= $CNum[0][0] ;
  54. }
  55. }
  56. }
  57. // Tens digit
  58. if ($s[1] !== '') {
  59. $s[1] = (int) $s[1];
  60. if ($s[1 ] !== 0) {
  61. $j .= $CNum[0][$s[1]].$CNum[1][1];
  62. } else {
  63. if ($s[0]!=0 && $ s[2] != 0) {
  64. $j .= $CNum[0][$s[1]];
  65. }
  66. }
  67. }
  68. // ones digit
  69. if ($s[0] !== ' ') {
  70. $s[0] = (int) $s[0];
  71. if ($s[0] !== 0) {
  72. $j .= $CNum[0][$s[0] ] .$CNum[1][0];
  73. } else {
  74. // $j .= $CNum[0][0];
  75. }
  76. }
  77. $j.=$CNum[2][$ i /4];
  78. array_unshift($chinese, $j);
  79. }
  80. $chs = implode('', $chinese);
  81. if ($fl) {
  82. $chs .= 'point';
  83. for ( $i=0,$j=strlen($fl); $i<$j; $i++) {
  84. $t = (int)$fl[$i];
  85. $chs.= $str[0][ $ t];
  86. }
  87. }
  88. return $chs;
  89. }
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template