金额大写工具类

// 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();
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值