改寫 VB版本 的數字金額轉換

PS:中間零太多轉出來感覺怪怪的!啊~將就用XD!

function NumtoStr($num){
	$numc	="零,壹,貳,參,肆,伍,陸,柒,捌,玖";
	$unic	=",拾,佰,仟";
	$unic1	="元整,萬,億,兆,京";
	
	$numc_arr	=split("," , $numc);
	$unic_arr	=split("," , $unic);
	$unic1_arr	=split("," , $unic1);
	
	$i = str_replace(',','',$num);#取代逗號
	$c0 = 0;
	$str=array();
	do{
		$aa = 0;
		$c1 = 0;
		$s = "";
		#取最右邊四位數跑迴圈,不足四位就全取
		$lan=(strlen($i)>=4)?4:strlen($i);
		$j = substr($i, -$lan);
		while($j>0){
			$k = $j % 10;#取餘數
			if($k > 0){
				$aa = 1;
				$s = $numc_arr[$k] . $unic_arr[$c1] . $s ;
			}elseif ($k == 0){
				if($aa == 1)	$s = "0" . $s;
			}
			$j = intval($j / 10);#只取整數(商)
			$c1 += 1;
		}
		#轉成中文後丟入陣列,全部為零不加單位
		$str[$c0]=($s=='')?'':$s.$unic1_arr[$c0];
		#計算剩餘字串長度
		$count_len=strlen($i) - 4;
		$i=($count_len > 0 )?substr($i, 0, $count_len):'';

		$c0 += 1;
	}while($i!='');
	
	#組合陣列
	foreach($str as $v)	$string .= array_pop($str);

	#取代重複0->零
	$string=preg_replace('/0+/','零',$string);

	return $string;
}
創作者介紹

一直都很冷

Brian Wang 發表在 痞客邦 PIXNET 留言(0) 人氣()