public class MoneyFormat {
private StringBuffer sb = new StringBuffer();
private String bigLetter[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌","玖" };
private String unit[] = { "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾","佰", "仟", "万" };
/**
* 转换金额为大写
*
* @param String 小写金额
* @return String 大写金额
* @author dryzeng
*/
public String format(String inputMoney) {
if (inputMoney.length() > 13) {
sb.append("数字太大,无法显示!");
return sb.toString();
} else if (Double.valueOf(inputMoney) < 0) {
sb.append("错误,金额为负数!");
return sb.toString();
} else if (Double.valueOf(inputMoney) == 0) {
sb.append("零圆整");
return sb.toString();
}
// 取得小数点前的数字
String moneyf[] = String.valueOf(inputMoney).split("\\.")[0].split("");
//金额转换
for (int i = 1; i < moneyf.length; i++) {
sb.append(bigLetter[Integer.valueOf(moneyf[i])]);
sb.append(unit[moneyf.length - 1 - i]);
}
try {
// 取得小数点后的数字
String moneyb[] = String.valueOf(inputMoney).split("\\.")[1].split("");
sb.append(bigLetter[Integer.valueOf(moneyb[1])]);
sb.append("角");
} catch (Exception e) {}
//格式人性化
String finalsb = sb.toString();
finalsb = finalsb.replaceAll("零仟", "零");
finalsb = finalsb.replaceAll("零佰", "零");
finalsb = finalsb.replaceAll("零拾", "零");
finalsb = finalsb.replaceAll("零亿", "亿");
finalsb = finalsb.replaceAll("零万", "万");
finalsb = finalsb.replaceAll("零零零", "零");
finalsb = finalsb.replaceAll("零零", "零");
finalsb = finalsb.replaceAll("零圆", "圆");
finalsb = finalsb.replaceAll("亿万", "亿");
finalsb = finalsb.replaceAll("零角", "");
if ("圆".equals(finalsb.substring(0, 1))) {
finalsb = "零" + finalsb;
}
return finalsb;
}
}
|