private static String[] num = {"零","壹","貳","三","肆","伍","陸","柒","捌","玖"};
// 10進制單位
private static String[] dw = {"","拾","佰","仟"};
// 數值進度單位參考 http://www.douban.com/group/topic/5404723/ http://zhidao.baidu.com/question/51925487.html
// 1000進制單位
private static String[] dw2 = {"","萬","億","兆","京","垓","杼","穰","溝","澗","正","載","極","恆河沙","阿僧祇","那由他","不可思議","無量","大數"};
private static String[] dw1 = {"角","分"};
public static void main(String[] args) {
String test = "009900990000";
System.out.println(parse(test));
}
private static String parse(String str) {
// str = str.replaceAll("^(0+)(.*)", "$2").replaceAll("(.*?)(0+)$", "$1");
// if(str.indexOf(".") == 0) {
// str = "0"+str;
// }
// 判斷是否為合格數字
if(str.matches("^\\d+(\\.\\d+)?\\d*$")) {
String integer = "";
String decimal = "";
// 拆分成整數和小數部分
int pos = str.indexOf(".");
if(pos >=0 ) {
integer = str.substring(0,pos);
decimal = str.substring(pos+1);
decimal = decimal.replaceAll("(.*?)(0+)$", "$1");
} else {
integer = str;
}
integer = integer.replaceAll("^(0+)(.*)", "$2");
if(integer.isEmpty() && !decimal.isEmpty()) {
integer = "0";
}
// 解析整數字符串
String intStr = parseIntger(integer);
// 解析小數字符串
String decimalStr = parseDecimal(decimal,!intStr.isEmpty());
if(!intStr.isEmpty()) {
intStr += "元";
if(integer.endsWith("0") && !decimal.isEmpty() && !decimal.startsWith("0")){
intStr+="零";
}
} else if(decimalStr.isEmpty()) {
intStr = "零元";
}
return intStr + (decimalStr.isEmpty()?"整":decimalStr);
}
throw new IllegalArgumentException(str);
}
/**
* 解析整數 規則為:
* 將字符串從後往前分割成四個字符一組的字符串二維數組(對應1000進制單位)
* 分別解析每個數組為10進制單位中對應的字符串
* 合並到一起則為整個字符串
* @param str
* @return
*/
private static String parseIntger(String str) {
if(null == str || str.isEmpty()) {
return "";
}
// 分割字符串為四個字符一組的字符串二維數組