Created
June 4, 2014 18:10
-
-
Save taichunmin/254c209696c02cd18538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 完成 阿拉伯數字 轉 中文數字 | |
* | |
* @author taichunmin <[email protected]> | |
*/ | |
function chineseNumber($num) | |
{ | |
if(empty($num)) return '零'; | |
$num = strrev(''.intval($num)); // 轉為字串並反轉 | |
$strl = strlen($num); | |
static $w = array('零','一','二','三','四','五','六','七','八','九'); | |
static $u1 = array('','十','百','千'); | |
static $u2 = array('','萬','億','兆'); | |
$res = ''; | |
for($i=$strl-1; $i>=0;$i--) | |
{ | |
if($num[$i]!='0') // 遇到零直接跳過 | |
{ | |
if($i<$strl-1 && $num[$i+1]=='0') $res.=$w[0]; | |
if($i==$strl-1 && $num[$i]=='1' && $i%4==1) | |
$res .= $u1[ $i%4 ]; // 處理 "一十" 省略一的特例 | |
else $res .= $w[ intval($num[$i]) ].$u1[ $i%4 ]; | |
} | |
if($i%4==0) | |
{ | |
$u2out = false; | |
for( $j=0; $j<4 && $i+$j<$strl; $j++) | |
$u2out |= ($num[$i+$j]!='0'); | |
if($u2out)$res .= $u2[$i/4]; | |
} | |
} | |
return $res; | |
} | |
/* | |
for($i=0;$i<=201;$i++) | |
echo chineseNumber($i).'<br />'; | |
$sa = '1'; | |
for($i=0;$i<=9;$i++) | |
{ | |
echo chineseNumber($sa.'1').'<br />'; | |
$sa.='0'; | |
} | |
//*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment