To convert numbers into Chinese characters
I wrote one myself
- function number2Chinese($num, $m = 1) {
- switch($m) {
- case 0:
- $CNum = array(
- array('zero','壹','二','三','四','五','鲁','旒','捌','玖'),
- array('','十','百','千'),
- array(' ','million','billion','trillion')
- );
- break;
- default:
- $CNum = array(
- array('zero','one','two','three',' four','five','six','seven','eight','nine'),
- array('','ten','hundred','thousand'),
- array('',' Wan','billion','trillion')
- );
- break;
- }
-
- // $cNum = array('zero','one','two','three','four',' five','six','seven','eight','nine');
-
- if (is_integer($num)) {
- $int = (string)$num;
- } else if (is_numeric($num )) {
- $num = explode('.', (string)floatval($num));
- $int = $num[0];
- $fl = isset($num[1]) ? $num[1 ] : FALSE;
- }
-
- // Length
- $len = strlen($int);
-
- // Chinese
- $chinese = array();
-
- // Reverse number
- $str = strrev($int );
-
- for($i = 0; $i<$len; $i+=4 ) {
- $s = array(0=>$str[$i], 1=>$str[$i+ 1 ], 2=>$str[$i+2], 3=>$str[$i+3]);
-
- $j = '';
- // Thousands digit
- if ($s[3 ] !== '') {
- $s[3] = (int) $s[3];
- if ($s[3] !== 0) {
- $j .= $CNum[0][$ s [3]].$CNum[1][3];
- } else {
- if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0 ) {
- $j .= $CNum[0][0];
- }
- }
- }
- // Hundreds place
- if ($s[2] !== '') {
- $s[2] = ( int ) $s[2];
- if ($s[2] !== 0) {
- $j .= $CNum[0][$s[2]].$CNum[1][2];
- } else {
- if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
- $j .= $CNum[0][0] ;
- }
- }
- }
- // Tens digit
- if ($s[1] !== '') {
- $s[1] = (int) $s[1];
- if ($s[1 ] !== 0) {
- $j .= $CNum[0][$s[1]].$CNum[1][1];
- } else {
- if ($s[0]!=0 && $ s[2] != 0) {
- $j .= $CNum[0][$s[1]];
- }
- }
- }
- // ones digit
- if ($s[0] !== ' ') {
- $s[0] = (int) $s[0];
- if ($s[0] !== 0) {
- $j .= $CNum[0][$s[0] ] .$CNum[1][0];
- } else {
-
- // $j .= $CNum[0][0];
-
- }
- }
-
- $j.=$CNum[2][$ i /4];
- array_unshift($chinese, $j);
- }
-
- $chs = implode('', $chinese);
-
- if ($fl) {
- $chs .= 'point';
- for ( $i=0,$j=strlen($fl); $i<$j; $i++) {
- $t = (int)$fl[$i];
- $chs.= $str[0][ $ t];
- }
- }
-
- return $chs;
- }
Copy code
- function number2Chinese($num, $m = 1) {
- switch($m) {
- case 0:
- $CNum = array(
- array('zero','壹','二','三','四','五','鲁','旒','捌','玖'),
- array('','十','百','千'),
- array(' ','million','billion','trillion')
- );
- break;
- default:
- $CNum = array(
- array('zero','one','two','three',' four','five','six','seven','eight','nine'),
- array('','ten','hundred','thousand'),
- array('',' Wan','billion','trillion')
- );
- break;
- }
-
- // $cNum = array('zero','one','two','three','four',' five','six','seven','eight','nine');
-
- if (is_integer($num)) {
- $int = (string)$num;
- } else if (is_numeric($num )) {
- $num = explode('.', (string)floatval($num));
- $int = $num[0];
- $fl = isset($num[1]) ? $num[1 ] : FALSE;
- }
-
- // Length
- $len = strlen($int);
-
- // Chinese
- $chinese = array();
-
- // Reverse number
- $str = strrev($int );
-
- for($i = 0; $i<$len; $i+=4 ) {
- $s = array(0=>$str[$i], 1=>$str[$i+ 1 ], 2=>$str[$i+2], 3=>$str[$i+3]);
-
- $j = '';
- // Thousands digit
- if ($s[3 ] !== '') {
- $s[3] = (int) $s[3];
- if ($s[3] !== 0) {
- $j .= $CNum[0][$ s [3]].$CNum[1][3];
- } else {
- if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0 ) {
- $j .= $CNum[0][0];
- }
- }
- }
- // Hundreds place
- if ($s[2] !== '') {
- $s[2] = ( int ) $s[2];
- if ($s[2] !== 0) {
- $j .= $CNum[0][$s[2]].$CNum[1][2];
- } else {
- if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
- $j .= $CNum[0][0] ;
- }
- }
- }
- // Tens digit
- if ($s[1] !== '') {
- $s[1] = (int) $s[1];
- if ($s[1 ] !== 0) {
- $j .= $CNum[0][$s[1]].$CNum[1][1];
- } else {
- if ($s[0]!=0 && $ s[2] != 0) {
- $j .= $CNum[0][$s[1]];
- }
- }
- }
- // ones digit
- if ($s[0] !== ' ') {
- $s[0] = (int) $s[0];
- if ($s[0] !== 0) {
- $j .= $CNum[0][$s[0] ] .$CNum[1][0];
- } else {
-
- // $j .= $CNum[0][0];
-
- }
- }
-
- $j.=$CNum[2][$ i /4];
- array_unshift($chinese, $j);
- }
-
- $chs = implode('', $chinese);
-
- if ($fl) {
- $chs .= 'point';
- for ( $i=0,$j=strlen($fl); $i<$j; $i++) {
- $t = (int)$fl[$i];
- $chs.= $str[0][ $ t];
- }
- }
-
- return $chs;
- }
Copy code
|