java针对数字进行大写转换

public class DigitUtil {

    private static String[] bigNums = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static String[] bigUnit = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
    private static String[] countNum = {"角", "分"};

    /**
     * 数字转中文大写
     *
     * @param money
     * @return
     */
    public static String numberToBig(String money){
        // 过滤空
        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() > bigUnit.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(bigUnit[dw]);
                }
            } else {
                if (isZero && i != 0) {
                    // 多个0只显示一个
                    res.append(bigNums[0]);
                }
                isZero = false;
                res.append(bigNums[index]);
                res.append(bigUnit[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(bigNums[0]);
                    }
                    isZero = false;
                    res.append(bigNums[index]);
                    res.append(countNum[i]);
                }
            }
        }
        return res.toString();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值