首頁  >  文章  >  後端開發  >  阿拉伯數字轉換為中文漢字(大、小寫)

阿拉伯數字轉換為中文漢字(大、小寫)

WBOY
WBOY原創
2016-07-25 09:01:441611瀏覽
要把數字轉成漢字
自己寫了一個
  1. function number2Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. case 0:
  6. $CNum = array(
  7. array('零','壹','貳','叁','肆','伍','陸','柒','捌','玖'),
  8. array('' ,'拾','佰','仟'),
  9. array('','萬','億','萬億')
  10. );
  11. break;
  12. default:
  13. $CNum = array(
  14. array('零','一','二','三','四','五','六','七','八','九'),
  15. array('','十','百','千'),
  16. array('','萬','億','萬億')
  17. ) ;
  18. break;
  19. }
  20. // $cNum = array('零','一','二','三','四','五','六' ,'七','八','九');
  21. if (is_integer($num)) {
  22. $int = (string)$num;
  23. } else if (is_numeric( $num)) {
  24. $num = explode('.', (string)floatval($num));
  25. $int = $num[0];
  26. $fl = isset($num[ 1]) ? $num[1] : FALSE;
  27. }
  28. // 長度
  29. $len = strlen($int);
  30. // 中文
  31. $ chinese = array();
  32. // 反轉的數字
  33. $str = strrev($int);
  34. for($i = 0; $i $s = array(0=>$str[$i], 1=>$str[$i+1], 2=>$str[$i+2], 3=> $str[$i+3]);
  35. $j = '';
  36. // 千位
  37. if ($s[3] !== '') {
  38. $ s[3] = (int) $s[3];
  39. if ($s[3] !== 0) {
  40. $j .= $CNum[0][$s[ 3]]. $CNum[1][3];
  41. } else {
  42. if ($s[2] != 0 || $s[1] != 0 || $s[0]! =0) {
  43. $j .= $CNum[0][0];
  44. }
  45. }
  46. }
  47. // 百位
  48. if ($s[2] ! == '' ) {
  49. $s[2] = (int) $s[2];
  50. if ($s[2] !== 0) {
  51. $j .= $CNum[ 0][$ s[2]].$CNum[1][2];
  52. } else {
  53. if ($s[3]!=0 && ($s[1] != 0 || $s[0 ]!=0) ) {
  54. $j .= $CNum[0][0];
  55. }
  56. }
  57. }
  58. // 十位
  59. if ($s[ 1] !== '') {
  60. $s[1] = (int) $s[1];
  61. if ($s[1] !== 0) {
  62. $j .= $CNum[0][$s[1]].$CNum[1][1];
  63. } else {
  64. if ($s[0]!=0 && $s[2 ] != 0 ) {
  65. $j .= $CNum[0][$s[1]];
  66. }
  67. }
  68. }
  69. // 位元
  70. if ( $s[0 ] !== '') {
  71. $s[0] = (int) $s[0];
  72. if ($s[0] !== 0) {
  73. $ j .= $ CNum[0][$s[0]].$CNum[1][0];
  74. } else {
  75. // $j .= $CNum[0][0 ];
  76. }
  77. }
  78. $j.=$CNum[2][$i/4];
  79. array_unshift($chinese, $j);
  80. }
  81. $chs = implode('', $chinese);
  82. if ($fl) {
  83. $chs .= '點';
  84. for($i= 0,$j =strlen($fl); $i $t = (int)$fl[$i];
  85. $chs.= $str[0][ $t];
  86. }
  87. }
return $chs;
}
複製程式碼
  1. function number2Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. case 0:
  6. $CNum = array(
  7. array('零','壹','貳','叁','肆','伍','陸','柒','捌','玖'),
  8. array('' ,'拾','佰','仟'),
  9. array('','萬','億','萬億')
  10. );
  11. break;
  12. default:
  13. $CNum = array(
  14. array('零','一','二','三','四','五','六','七','八','九'),
  15. array('','十','百','千'),
  16. array('','萬','億','萬億')
  17. ) ;
  18. break;
  19. }
  20. // $cNum = array('零','一','二','三','四','五','六' ,'七','八','九');
  21. if (is_integer($num)) {
  22. $int = (string)$num;
  23. } else if (is_numeric( $num)) {
  24. $num = explode('.', (string)floatval($num));
  25. $int = $num[0];
  26. $fl = isset($num[ 1]) ? $num[1] : FALSE;
  27. }
  28. // 長度
  29. $len = strlen($int);
  30. // 中文
  31. $ chinese = array();
  32. // 反轉的數字
  33. $str = strrev($int);
  34. for($i = 0; $i $s = array(0=>$str[$i], 1=>$str[$i+1], 2=>$str[$i+2], 3=> $str[$i+3]);
  35. $j = '';
  36. // 千位
  37. if ($s[3] !== '') {
  38. $ s[3] = (int) $s[3];
  39. if ($s[3] !== 0) {
  40. $j .= $CNum[0][$s[ 3]]. $CNum[1][3];
  41. } else {
  42. if ($s[2] != 0 || $s[1] != 0 || $s[0]! =0) {
  43. $j .= $CNum[0][0];
  44. }
  45. }
  46. }
  47. // 百位
  48. if ($s[2] ! == '' ) {
  49. $s[2] = (int) $s[2];
  50. if ($s[2] !== 0) {
  51. $j .= $CNum[ 0][$ s[2]].$CNum[1][2];
  52. } else {
  53. if ($s[3]!=0 && ($s[1] != 0 || $s[0 ]!=0) ) {
  54. $j .= $CNum[0][0];
  55. }
  56. }
  57. }
  58. // 十位
  59. if ($s[ 1] !== '') {
  60. $s[1] = (int) $s[1];
  61. if ($s[1] !== 0) {
  62. $j .= $CNum[0][$s[1]].$CNum[1][1];
  63. } else {
  64. if ($s[0]!=0 && $s[2 ] != 0 ) {
  65. $j .= $CNum[0][$s[1]];
  66. }
  67. }
  68. }
  69. // 位元
  70. if ( $s[0 ] !== '') {
  71. $s[0] = (int) $s[0];
  72. if ($s[0] !== 0) {
  73. $ j .= $ CNum[0][$s[0]].$CNum[1][0];
  74. } else {
  75. // $j .= $CNum[0][0 ];
  76. }
  77. }
  78. $j.=$CNum[2][$i/4];
  79. array_unshift($chinese, $j);
  80. }
  81. $chs = implode('', $chinese);
  82. if ($fl) {
  83. $chs .= '點';
  84. for($i= 0,$j =strlen($fl); $i $t = (int)$fl[$i];
  85. $chs.= $str[0][ $t];
  86. }
  87. }
return $chs;
}
複製程式碼
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn