- /**
- * Convert numbers to Chinese
- * @param string|integer|float $num target number
- * @param integer $mode mode [true: amount (default), false: ordinary number representation]
- * @param boolean $sim use lowercase (Default)
- * @return string
- */
- function number2chinese($num,$mode = true,$sim = true){
- if(!is_numeric($num)) return 'contains non-numbers Non-decimal point character! ';
- $char = $sim ? array('zero','one','two','three','four','five','six','seven','eight','nine' )
- : array('zero','one','two','three','four','five','Lu','旒','捌','玖');
- $unit = $sim ? array('','十','hundred','thousand','','ten thousand','billion','trillion')
- : array('','十','hundred', '仟','','万','hundred million','trillion');
- $retval = $mode ? 'Yuan':'point';
-
- //Decimal part
- if(strpos($num, ' .')){
- list($num,$dec) = explode('.', $num);
- $dec = strval(round($dec,2));
- if($mode){
- $retval .= "{$char[$dec['0']]}angle {$char[$dec['1']]}cent";
- }else{
- for($i = 0,$c = strlen( $dec);$i < $c;$i++) {
- $retval .= $char[$dec[$i]];
- }
- }
- }
- //Integer part
- $str = $mode ? strrev (intval($num)) : strrev($num);
- for($i = 0,$c = strlen($str);$i < $c;$i++) {
- $out[$i] = $char[$str[$i]];
- if($mode){
- $out[$i] .= $str[$i] != '0'? $unit[$i%4] : '' ;
- if($i>1 and $str[$i]+$str[$i-1] == 0){
- $out[$i] = '';
- }
- if($i%4 = = 0){
- $out[$i] .= $unit[4+floor($i/4)];
- }
- }
- }
- $retval = join('',array_reverse($out)) . $ retval;
- return $retval;
- }
-
- //Instance call==================================== ==================
- $num = '0123648867.789';
- echo $num,'
';
- //Chinese character representation of ordinary numbers
- echo 'Ordinary :',number2chinese($num,false),'';
- echo '
';
- //Amount expressed in Chinese characters
- echo 'Amount (simplified):',number2chinese($num,true),'';
- echo '
';
- echo 'Amount (Traditional):',number2chinese($num,true,false);
-
- ?>
Copy code
|