// An highlighted block
public class MoneyUtil {
// 大写数字
private static final String[] NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
// 整数部分的单位
private static final String[] IUNIT = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟"};
// 小数部分的单位
private static final String[] DUNIT = {"角", "分", "厘"};
/**
* 特殊字符:整
*/
private static final String CN_FULL = "整";
/**
* 特殊字符:负
*/
private static final String CN_NEGATIVE = "负";
/**
*
*
* @description:金额转大写
* @author
* @version V0.1
* @date
**/
public static String number2CNMontray(BigDecimal moneyNumber) {
StringBuffer sb = new StringBuffer();
boolean isNegative = false;
if (moneyNumber == null || moneyNumber.compareTo(new BigDecimal(0)) == 0) {
return NUMBERS[0] + IUNIT[0] + CN_FULL;
}else if(moneyNumber.compareTo(new BigDecimal(0)) < 0){
isNegative = true;
moneyNumber = moneyNumber.abs();
}
// 整数部分
Long lv = moneyNumber.longValue();
int zeroLength = 0;
for (int numIndex = 0; lv > 0; numIndex++) {
int num = (int) (lv % 10);
lv = lv / 10;
if (num == 0) {
if ((numIndex + 1) != (++zeroLength) && zeroLength == 1) {
sb.insert(0, NUMBERS[num]);
}
} else {
if (numIndex > 4 && numIndex == zeroLength && zeroLength != 0) {
sb.insert(0, IUNIT[0]);
}
if (numIndex % 4 != 0 && zeroLength > 0) {
sb.insert(0, IUNIT[((numIndex / 4) * 4)]);
}
zeroLength = 0;
sb.insert(0, NUMBERS[num] + IUNIT[numIndex]);
}
}
// 小数部分
// 数字转为字符
String plain = moneyNumber.toPlainString(), decimalStr = "";
int decimalPointIndex = plain.indexOf('.');
zeroLength = 0;
if (decimalPointIndex == -1 || StringUtils.isEmpty(decimalStr = plain.substring(decimalPointIndex + 1)
.replaceAll("0*$", ""))) {
sb.append(CN_FULL);
} else {
if (decimalStr.length() > DUNIT.length) {
decimalStr = decimalStr.substring(0, DUNIT.length);
}
for (int i = 0; i < decimalStr.length(); i++) {
if ('0' != decimalStr.charAt(i)) {
sb.append(NUMBERS[(int) decimalStr.charAt(i) - 48] + DUNIT[i]);
}else if((++zeroLength) < 2 && moneyNumber.longValue() != 0){
sb.append(NUMBERS[0]);
}
}
}
if(isNegative){
sb.insert(0, CN_NEGATIVE);
}
return sb.toString();
}
}
金额大写工具类
最新推荐文章于 2024-06-17 14:07:47 发布