一、金额大写
public static String convertChinese(BigDecimal amonut) {
String money = amonut.setScale(2,BigDecimal.ROUND_HALF_UP).toPlainString();
// 过滤空
if (money == null || "".equals(money)) {
return "";
}
StringBuilder res = new StringBuilder();
String[] splitStr = money.split("\\.");
if (splitStr.length > 2) {
throw new RuntimeException("输入的参数不是数字!");
}
String front = splitStr[0];
// 用于判定0的显示
boolean isZero = true;
if (front.length() > unit.length) {
throw new RuntimeException("输入的参数大于万亿!");
}
for (int i = 0; i < front.length(); i++) {
// 整数位处理
int dw = front.length() - i - 1;
// 用ASCII码获得数字
int index = (front.charAt(i) - '0');
if (index == 0) {
isZero = true;
if (dw == 0 || dw == 4 || dw == 8) {
// 元,万,亿需要拼接
res.append(unit[dw]);
}
} else {
if (isZero && i != 0) {
// 多个0只显示一个
res.append(CN_UPPER_NUMBER[0]);
}
isZero = false;
res.append(CN_UPPER_NUMBER[index]);
res.append(unit[dw]);
}
}
// 判断是否有小数位
if (splitStr.length > 1) {
// 小数位处理,如果不需要则直接删除这个if及内部所有内容
isZero = true;
String back = splitStr[1];
if (back.length() > countNum.length) {
throw new RuntimeException("小数位小于【" + countNum[countNum.length - 1] + "】!");
}
for (int i = 0; i < back.length(); i++) {
int index = (back.charAt(i) - '0');
if (index == 0) {
isZero = true;
} else {
if (isZero && i != 0) {
res.append(CN_UPPER_NUMBER[0]);
}
isZero = false;
res.append(CN_UPPER_NUMBER[index]);
res.append(countNum[i]);
}
}
}
if (splitStr.length == 2 && "00".equals(splitStr[1])) {
res.append(CN_FULL);
}
return res.toString();
}
二、金额三位一逗两位小数
public static String covertRMBStr(BigDecimal amt){
if(Objects.isNull(amt)){
amt = BigDecimal.ZERO;
}
return String.format("%,.2f",amt);
//return String.format("【%,.2f】",amt);//带括号
}