Heim  >  Artikel  >  Backend-Entwicklung  >  阿拉伯数字转化为中文汉字(大、小写)

阿拉伯数字转化为中文汉字(大、小写)

WBOY
WBOYOriginal
2016-07-25 09:01:441613Durchsuche
要把数字转成汉字
自己写了一个
  1. function number2Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'),
  6. array('','拾','佰','仟'),
  7. array('','萬','億','萬億')
  8. );
  9. break;
  10. default:
  11. $CNum = array(
  12. array('零','一','二','三','四','五','六','七','八','九'),
  13. array('','十','百','千'),
  14. array('','万','亿','万亿')
  15. );
  16. break;
  17. }
  18. // $cNum = array('零','一','二','三','四','五','六','七','八','九');
  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. // 长度
  27. $len = strlen($int);
  28. // 中文
  29. $chinese = array();
  30. // 反转的数字
  31. $str = strrev($int);
  32. for($i = 0; $i $s = array(0=>$str[$i], 1=>$str[$i+1], 2=>$str[$i+2], 3=>$str[$i+3]);
  33. $j = '';
  34. // 千位
  35. if ($s[3] !== '') {
  36. $s[3] = (int) $s[3];
  37. if ($s[3] !== 0) {
  38. $j .= $CNum[0][$s[3]].$CNum[1][3];
  39. } else {
  40. if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0) {
  41. $j .= $CNum[0][0];
  42. }
  43. }
  44. }
  45. // 百位
  46. if ($s[2] !== '') {
  47. $s[2] = (int) $s[2];
  48. if ($s[2] !== 0) {
  49. $j .= $CNum[0][$s[2]].$CNum[1][2];
  50. } else {
  51. if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
  52. $j .= $CNum[0][0];
  53. }
  54. }
  55. }
  56. // 十位
  57. if ($s[1] !== '') {
  58. $s[1] = (int) $s[1];
  59. if ($s[1] !== 0) {
  60. $j .= $CNum[0][$s[1]].$CNum[1][1];
  61. } else {
  62. if ($s[0]!=0 && $s[2] != 0) {
  63. $j .= $CNum[0][$s[1]];
  64. }
  65. }
  66. }
  67. // 个位
  68. if ($s[0] !== '') {
  69. $s[0] = (int) $s[0];
  70. if ($s[0] !== 0) {
  71. $j .= $CNum[0][$s[0]].$CNum[1][0];
  72. } else {
  73. // $j .= $CNum[0][0];
  74. }
  75. }
  76. $j.=$CNum[2][$i/4];
  77. array_unshift($chinese, $j);
  78. }
  79. $chs = implode('', $chinese);
  80. if ($fl) {
  81. $chs .= '点';
  82. for($i=0,$j=strlen($fl); $i $t = (int)$fl[$i];
  83. $chs.= $str[0][$t];
  84. }
  85. }
  86. return $chs;
  87. }
复制代码
  1. function number2Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'),
  6. array('','拾','佰','仟'),
  7. array('','萬','億','萬億')
  8. );
  9. break;
  10. default:
  11. $CNum = array(
  12. array('零','一','二','三','四','五','六','七','八','九'),
  13. array('','十','百','千'),
  14. array('','万','亿','万亿')
  15. );
  16. break;
  17. }
  18. // $cNum = array('零','一','二','三','四','五','六','七','八','九');
  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. // 长度
  27. $len = strlen($int);
  28. // 中文
  29. $chinese = array();
  30. // 反转的数字
  31. $str = strrev($int);
  32. for($i = 0; $i $s = array(0=>$str[$i], 1=>$str[$i+1], 2=>$str[$i+2], 3=>$str[$i+3]);
  33. $j = '';
  34. // 千位
  35. if ($s[3] !== '') {
  36. $s[3] = (int) $s[3];
  37. if ($s[3] !== 0) {
  38. $j .= $CNum[0][$s[3]].$CNum[1][3];
  39. } else {
  40. if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0) {
  41. $j .= $CNum[0][0];
  42. }
  43. }
  44. }
  45. // 百位
  46. if ($s[2] !== '') {
  47. $s[2] = (int) $s[2];
  48. if ($s[2] !== 0) {
  49. $j .= $CNum[0][$s[2]].$CNum[1][2];
  50. } else {
  51. if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
  52. $j .= $CNum[0][0];
  53. }
  54. }
  55. }
  56. // 十位
  57. if ($s[1] !== '') {
  58. $s[1] = (int) $s[1];
  59. if ($s[1] !== 0) {
  60. $j .= $CNum[0][$s[1]].$CNum[1][1];
  61. } else {
  62. if ($s[0]!=0 && $s[2] != 0) {
  63. $j .= $CNum[0][$s[1]];
  64. }
  65. }
  66. }
  67. // 个位
  68. if ($s[0] !== '') {
  69. $s[0] = (int) $s[0];
  70. if ($s[0] !== 0) {
  71. $j .= $CNum[0][$s[0]].$CNum[1][0];
  72. } else {
  73. // $j .= $CNum[0][0];
  74. }
  75. }
  76. $j.=$CNum[2][$i/4];
  77. array_unshift($chinese, $j);
  78. }
  79. $chs = implode('', $chinese);
  80. if ($fl) {
  81. $chs .= '点';
  82. for($i=0,$j=strlen($fl); $i $t = (int)$fl[$i];
  83. $chs.= $str[0][$t];
  84. }
  85. }
  86. return $chs;
  87. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn