Contents ...
udn網路城邦
[java] 將阿拉伯數字轉換成中文
2009/07/08 13:10
瀏覽3,630
迴響0
推薦0
引用0
/**
 * 將阿拉伯數字轉換成中文
 * */
public String numToCht(String intString){
 int chtNumLength = 16;
 String fullTypeSapce = " ";
 final String[] CHT_NUM = {"零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖"};
 String[] newChtNum = new String[chtNumLength];
 String[] chtNum = new String[chtNumLength];
   chtNum[0]   = fullTypeSapce;
   chtNum[1]   = "仟";
   chtNum[2]   = fullTypeSapce;
   chtNum[3]   = "佰";
   chtNum[4]   = fullTypeSapce;
   chtNum[5]   = "拾";
   chtNum[6]   = fullTypeSapce;
   chtNum[7]   = "萬";
   chtNum[8]   = fullTypeSapce;
   chtNum[9]   = "仟";
   chtNum[10]  = fullTypeSapce;
   chtNum[11]  = "佰";
   chtNum[12]  = fullTypeSapce;
   chtNum[13]  = "拾";
   chtNum[14]  = "";
   chtNum[15]  = "元";
 try{
  /* 判斷是否為正整數 */
  Integer.parseInt(intString);
  
  /* 將阿拉伯數字轉換成中文, 再存入 String Array */
  int strLength = intString.length();
  int index = chtNumLength - strLength - strLength;
  for(int i=0; i   String indexValue = intString.substring(i, i+1);
   newChtNum[i +i] = CHT_NUM[Integer.parseInt(indexValue)];
   newChtNum[i +i+1] = chtNum[index +i +i+1];
  }       
 }catch(Exception e){
  System.out.println("It is not integer::" + intString);
 }
 
 /* 將 String Array 重新組成字串 */
 StringBuffer sbCHT = new StringBuffer();
 for(int i=0; i  if(newChtNum[i]!=null)
   sbCHT.append(newChtNum[i]);
 } 
 return sbCHT.toString();
}
發表迴響

會員登入