java将金额转化为大写金额
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 "";
}
// 分割字符串为四个字符一组的字符串二维数组
本文原创,转载必追究版权。