日期:2014-05-20 浏览次数:20710 次
package com.zf.test;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class Test4 {
private Map<String , String> e2c = new HashMap<String, String>();
{
e2c.put("0", "零");
e2c.put("1", "一");
e2c.put("2", "二");
e2c.put("3", "三");
e2c.put("4", "四");
e2c.put("5", "五");
e2c.put("6", "六");
e2c.put("7", "七");
e2c.put("8", "八");
e2c.put("9", "九");
}
private String[] ws = new String[]{" " ,"十" , "百" ,"千" ,"万" , "十万" ,"百万" ,"千万" ,"亿" ,"十亿" ,"百亿" ,"千亿"};
private String str ; //要转换的字符串
private LinkedList<String> beforePoint ; //保存小数点之前的内容
private String afterPoint ; //保存小数点之后的内容
public Test4(String str){
this.str = str ;
String[] tmp = str.split("\\.");
beforePoint = new LinkedList<String>();
char[] cs = tmp[0].toCharArray();
for (Character c : cs) {
beforePoint.add(String.valueOf(c));
}
if(tmp.length > 1)
afterPoint = tmp[1] ;
}
private String translate(){
int currentIndex = 0 ;
StringBuilder sb = new StringBuilder();
String lastNumber = "0";
String lastFh = " ";
while(!beforePoint.isEmpty()){
String s = beforePoint.removeLast();
String fh = ws[currentIndex] ;
if( lastNumber.equals("0") && s.equals("0")){
if(beforePoint.isEmpty())
sb.insert(0, e2c.get(s));
}else {
sb.insert(0, e2c.get(s));
if(!s.equals("0")){
if(lastFh.charAt(lastFh.length() - 1) == fh.charAt(fh.length() - 1))
fh = fh.substring(0 , fh.length() - 1);
sb.insert( 1, fh);
}
}
lastNumber = s;
lastFh = s.equals("0") ? " " : ws[currentIndex];
currentIndex++;
}
return sb.toString();
}
private String format(){
String before = translate();
StringBuilder after = new StringBuilder();
if(afterPoint != null){
before += "点";
for (int i = 0; i < afterPoint.length() ; i++) {
after.append(e2c.get(String.valueOf(afterPoint.charAt(i))));