直接列出代码如下:
package com.li.project.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* @author li
*/
public class MoneyUtil {
private static final String[] Words = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
private static final String[] Units = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万" };
public static final DecimalFormat df_money;
static {
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.getDefault());
decimalFormatSymbols.setDecimalSeparator('.');
decimalFormatSymbols.setGroupingSeparator(',');
decimalFormatSymbols.setMonetaryDecimalSeparator('.');
df_money = new DecimalFormat("0.00", decimalFormatSymbols);
}
/**
* 转大写中文金额
*/
public static String toChinese(BigDecimal amount) {
return toChinese(df_money.format(amount));
}
public static String toChinese(String moneyStr) {
if (moneyStr == null) {
return "";
}
BigDecimal amt = new BigDecimal(moneyStr);
BigDecimal Money = amt.abs();
StringBuilder sb = new StringBuilder();
if (amt.compareTo(BigDecimal.ZERO) < 0) {
sb.append("负");
}
BigDecimal iMoney, fMoney, Jiao, Fen, Zero = new BigDecimal(0), Ten = new BigDecimal(10);
int i;
int[] Digits = new int[13];
Money = Money.setScale(2, RoundingMode.HALF_UP);
iMoney = Money.setScale(0, RoundingMode.DOWN);
fMoney = Money.subtract(iMoney).multiply(new BigDecimal("100.00"));
Jiao = fMoney.divide(Ten, 0, RoundingMode.DOWN);
Fen = fMoney.subtract(Jiao.multiply(Ten));
if (iMoney.compareTo(new BigDecimal("10000000000000.00")) >= 0) {
return "超出表示范围";
}
i = 0;
while (iMoney.compareTo(Zero) != 0) {
Digits[i] = iMoney.subtract(iMoney.divide(Ten, 0, RoundingMode.DOWN).multiply(Ten)).intValue();
iMoney = iMoney.divide(Ten, 0, RoundingMode.DOWN);
++i;
}
--i;
int nz = 0;
while (i >= 0) {
if (Digits[i] != 0) {
sb.append(Words[Digits[i]]).append(Units[i]);
nz = i;
} else {
if (i % 4 != 0) {
if (Digits[i - 1] != 0) {
sb.append(Words[Digits[i]]);
}
} else {
if (i == 0 || nz != 8) {
sb.append(Units[i]);
}
}
}
--i;
}
if (Jiao.intValue() != 0) {
sb.append(Words[Jiao.intValue()]).append("角");
} else if ((Fen.intValue() != 0) && (sb.length() > 0)) {
sb.append("零");
}
if (Fen.intValue() != 0) {
sb.append(Words[Fen.intValue()]).append("分");
}
if (sb.length() == 0) {
sb.append("零元零角零分");
}
if (Jiao.intValue() == 0 && Fen.intValue() == 0) {
sb.append("整");
}
return sb.toString();
}
}