import org.junit.Test;
public class NumberFormat {
private static String[][] cnum={{"零","一","二","三","四","五","六","七","八","九","元","角","分"},
{"零","壹","貳","叁","肆","伍","陆","柒","捌","玖","圆","角","分"}};
private static String[][] dw={{"十","百","千","万","十","百","千","亿"},
{"拾","佰","仟","萬","拾","佰","仟","亿"}};
@Test
public void test(){
String s="1234567890123456789.01";
Long time=System.nanoTime();
System.out.println(s+"/n"+this.toChinese(s,false));
System.out.println(System.nanoTime()-time);
}
/***
*
* @param String numberstr需要转换的数字串
* @param flag boolean 简繁体
* @return
*/
public String toChinese(String numberstr,boolean flag){
int cnum_flag=flag==true?1:0;
//小数点的索引
int index_point=numberstr.indexOf(".");
if(index_point<0)
index_point=0;
StringBuffer sb=new StringBuffer();
String zhengshu=null;//整数位
String fenshu=null;//分数位
zhengshu=numberstr.substring(0,index_point);
int strLen=zhengshu.length();
for(int j=1;j<=strLen;j++){
char num=zhengshu.charAt(j-1);
toNum(sb,num,cnum_flag);
int i=strLen-j-1;
if(i!=-1){
sb.append(dw[cnum_flag][i%8]);
}
}
if(index_point>0){
fenshu=numberstr.substring(index_point+1);
sb.append(cnum[cnum_flag][10]);
for(int j=1;j<=fenshu.length();j++){
char num=fenshu.charAt(j-1);
toNum(sb,num,cnum_flag);
switch(j){
case 2:
sb.append(cnum[cnum_flag][12]);
break;
case 1:
sb.append(cnum[cnum_flag][11]);
break;
}
}
}
return sb.toString();
}
private void toNum(StringBuffer sb,char ch,int cnum_flag){
switch(ch){
case '0':
sb.append(cnum[cnum_flag][0]);
break;
case '1':
sb.append(cnum[cnum_flag][1]);
break;
case '2':
sb.append(cnum[cnum_flag][2]);
break;
case '3':
sb.append(cnum[cnum_flag][3]);
break;
case '4':
sb.append(cnum[cnum_flag][4]);
break;
case '5':
sb.append(cnum[cnum_flag][5]);
break;
case '6':
sb.append(cnum[cnum_flag][6]);
break;
case '7':
sb.append(cnum[cnum_flag][7]);
break;
case '8':
sb.append(cnum[cnum_flag][8]);
break;
case '9':
sb.append(cnum[cnum_flag][9]);
break;
}
}
}