Java实现大写金额转小写金额

转自:https://blog.csdn.net/rmnjava/article/details/24693733

public class MoneyUtil {

    /**
     * 中文中简写的汉字金额 经常使用
     */
    private static String[] rmbNumbers = new String[]{
            "一", "二", "三", "四", "五", "六", "七", "八", "九", "两", "廿", "卅", "○"};
    /**
     * 中文中繁写的汉字金额 经常使用
     */
    private static String[] bigNumbers = new String[]{
            //大写的汉字
            "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "俩", "廿", "卅", "零"};
    /**
     * 与汉字相应的转化的数字
     */
    private static Long[] tonumbers = new Long[]{
            //转化为阿拉伯数字
            1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L, 2L, 3L, 0L};

    /**
     * 倍数关键词 简写 注意:一定要由大到小
     */
    private static String[] rmbMult = new String[]{
            //大写中间隔的倍数
            "亿", "万", "千", "百", "十"};
    /**
     * 倍数关键词 繁写
     */
    private static String[] bigRmbMult = new String[]{
            "億", "萬", "仟", "佰", "拾"};

    /**
     * 与倍数关键词对应的倍数
     */
    private static Long[] toMult = new Long[]{
            //转化为阿拉伯的倍数
            100000000L, 10000L, 1000L, 100L, 10L};

    /**
     * 大写转化为小写的过程操作,只处理到元,不带有单位
     *
     * @param money 大写的金额,不带有单位 例如:1.二十一万 2.六五四三 3 贰拾
     * @return
     */
    public static String rmbBigToSmall(String money) {
        if (StringUtils.isEmpty(money)) {
            return "0";
        }
        //处理大写金额的元、整
        money = money.replace("元", "").replace("整", "");
        Long number = 0L;
        //遍历倍数的中文词遍历的时候一定要注意 选取的倍数词为最后一个倍数词,此次遍历为第一次遍历
        for (int i = 0; i < rmbMult.length; i++) {
            int index = money.lastIndexOf(rmbMult[i]) == -1 ? money.lastIndexOf(bigRmbMult[i]) : money.lastIndexOf(rmbMult[i]);
            if (index >= 0) {
                String storeMult = money.substring(0, index);
                money = money.substring(index + 1);
                //对于 十九万 这样的特殊的十的情况进行特殊处理
                if ((storeMult == null || storeMult.length() <= 0) && toMult[i].intValue() == 10) {
                    number = number + toMult[i];
                } else {
                    number = number + (toMult[i] * getPrexNum(storeMult));
                }
            }
        }
        /**
         * 个位数的处理
         */
        number = number + getNumByBig(money);
        return number.toString();
    }

    /**
     * 辅助类,第二次循环
     * 此循环一般处理的都是倍数前面的数字,例如十九万,在这里就处理十九
     *
     * @param storeMult 倍数前面的前缀词
     * @return
     */
    private static Long getPrexNum(String storeMult) {
        Long result = 0L;
        for (int i = 0; i < rmbMult.length; i++) {
            int index = storeMult.lastIndexOf(rmbMult[i]) == -1 ? storeMult.lastIndexOf(bigRmbMult[i]) : storeMult.lastIndexOf(rmbMult[i]);
            if (index >= 0) {
                String storeMult2 = storeMult.substring(0, index);
                storeMult = storeMult.substring(index + 1);
                if ((storeMult2 == null || storeMult2.length() <= 0) && toMult[i].intValue() == 10) {
                    result = result + toMult[i];
                } else {
                    result += getNumByBig(storeMult2) * toMult[i];
                }
            }
        }
        if (storeMult != null && storeMult.length() > 0) {
            result = result + getNumByBig(storeMult);
        }
        return result;
    }

    /**
     * 辅助类,大写的中文数字 转化为小写的阿拉伯数字
     *
     * @param big
     * @return
     */
    private static Long getNumByBig(String big) {
        Long result = 0L;
        for (int j = 0; j < rmbNumbers.length; j++) {
            big = big.replaceAll(rmbNumbers[j], tonumbers[j].toString());
            big = big.replaceAll(bigNumbers[j], tonumbers[j].toString());
        }
        try {
            result = Long.valueOf(big);
        } catch (Exception e) {
            result = 0L;
        }
        return result;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Java金额大写小写金额换方法,我可以给你提供以下代码: ```java public class MoneyUtil { private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"}; private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] CN_UPPER_NUMBER_MONETRAY_UNIT = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰", "仟"}; private static final String CN_FULL = "整"; private static final String CN_NEGATIVE = "负"; private static final String CN_ZERO = "零"; private static final double MAX_VALUE = 9999999999999.99D; public static double convertToDouble(String cnMoney) { if (cnMoney == null || cnMoney.isEmpty()) { return 0D; } cnMoney = cnMoney.replaceAll("元", "").replaceAll("整", ""); if (cnMoney.startsWith(CN_NEGATIVE)) { cnMoney = cnMoney.substring(1); } double result = 0D; double temp = 1D; boolean flag = false; for (int i = cnMoney.length() - 1; i >= 0; i--) { int number = getNumber(cnMoney.charAt(i)); if (number < 0) { if (i == 0) { result -= temp; } flag = true; continue; } if (flag) { result -= temp; temp *= 100D; flag = false; } if (number == 10 || number == 11 || number == 12) { temp = getUnit(number - 10); } else { result += temp * number; } } if (cnMoney.startsWith(CN_NEGATIVE)) { result = -result; } if (result < 0 || result > MAX_VALUE) { return 0D; } return result; } private static int getNumber(char c) { for (int i = 0; i < CN_UPPER_NUMBER_MONETRAY_UNIT.length; i++) { if (CN_UPPER_NUMBER_MONETRAY_UNIT[i].equals(String.valueOf(c))) { return i; } } return -1; } private static double getUnit(int index) { if (index == 0) { return 0.1D; } else if (index == 1) { return 0.01D; } else { return Math.pow(10D, index); } } } ``` 这段代码可以将大写金额换为小写金额。你可以将要换的大写金额作为参数传递给该方法,然后该方法将返回换后的小写金额。例如: ```java String cnMoney = "壹仟贰佰叁拾肆元伍角陆分"; double result = MoneyUtil.convertToDouble(cnMoney); System.out.println(result); ``` 输出结果为:1234.56。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值