JAVA 大写金额转数字

大写金额转数字

public Double CNYtoN(String amount) {
        double result = 0;
        double temp = -1;//存放一个单位的数字如:十万
        int count = 0;//判断是否有chArr
        Map<Character, Double> map = new HashMap<Character, Double>(); //存放数字map
        map.put('壹', 1.0);
        map.put('贰', 2.0);
        map.put('叁', 3.0);
        map.put('肆', 4.0);
        map.put('伍', 5.0);
        map.put('陆', 6.0);
        map.put('柒', 7.0);
        map.put('捌', 8.0);
        map.put('玖', 9.0);
        Map<Character, Double> map1 = new HashMap<Character, Double>(); //存放单位map
        map1.put('拾', 10.0);
        map1.put('佰', 100.0);
        map1.put('仟', 1000.0);
        map1.put('万', 10000.0);
        map1.put('亿', 100000000.0);
        map1.put('角', 0.1);
        map1.put('分', 0.01);
        map1.put('厘', 0.001);
        for (int i = 0; i < amount.length(); i++) { //遍历属组
            char c = amount.charAt(i);
            if (map.containsKey(c)) {
                if (temp != -1) {
                    result += temp;
                    temp = -1;
                }
                temp = map.get(c);
            } else {
                if (temp == -1) {
                    continue;
                }
                if (map1.containsKey(c)) {
                    temp *= map1.get(c);
                }
            }
            if (i == amount.length() - 1) {
                result += temp;
            }
        }
        return result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Java实现大写金额数字的示例代码: ```java public class AmountUtil { private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"}; private static final BigDecimal MAX_VALUE = new BigDecimal("9999999999999.99"); /** * 将大写金额换为数字形式 * * @param amount 大写金额 * @return 数字形式 */ public static BigDecimal convertToNumber(String amount) { if (!amount.matches("^[壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分]*$")) { throw new IllegalArgumentException("不是有效的大写金额格式"); } StringBuilder sb = new StringBuilder(amount); // 处理万、亿 int index = sb.indexOf("亿"); if (index > -1) { String temp = sb.substring(0, index); BigDecimal num = new BigDecimal(convertToNumber(temp).toString()); sb.delete(0, index + 1); sb.insert(0, " "); BigDecimal value = new BigDecimal(convertToNumber(sb.toString().trim()).toString()); return num.multiply(new BigDecimal("100000000")).add(value); } index = sb.indexOf("万"); if (index > -1) { String temp = sb.substring(0, index); BigDecimal num = new BigDecimal(convertToNumber(temp).toString()); sb.delete(0, index + 1); sb.insert(0, " "); BigDecimal value = new BigDecimal(convertToNumber(sb.toString().trim()).toString()); return num.multiply(new BigDecimal("10000")).add(value); } // 处理元、角、分 BigDecimal result = new BigDecimal("0"); int numIndex = 0; int unitIndex = 0; boolean getZero = false; while (numIndex < amount.length()) { char c = amount.charAt(numIndex); if (c >= '壹' && c <= '玖') { int num = c - '壹' + 1; if (unitIndex == 2) { result = result.add(new BigDecimal(num)); } else if (unitIndex == 6) { result = result.add(new BigDecimal(num).multiply(new BigDecimal("10000"))); } else if (unitIndex == 10) { result = result.add(new BigDecimal(num).multiply(new BigDecimal("100000000"))); } else { result = result.add(new BigDecimal(num).multiply(new BigDecimal(Math.pow(10, 3 - unitIndex)))); } getZero = false; } else if (c == '零') { getZero = true; } else if (CN_UPPER_MONETRAY_UNIT[unitIndex].equals(String.valueOf(c))) { getZero = false; } else if (c == '元') { getZero = false; } else if (c == '角') { if (getZero) { result = result.add(new BigDecimal("0.1")); } getZero = false; } else if (c == '分') { if (getZero) { result = result.add(new BigDecimal("0.01")); } getZero = false; } if (CN_UPPER_MONETRAY_UNIT[unitIndex].equals(String.valueOf(c))) { ++unitIndex; } ++numIndex; } return result; } public static void main(String[] args) { String amount = "壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元零壹分"; System.out.println(convertToNumber(amount)); // 123456789.01 } } ``` 以上代码可以将大写金额换为数字形式,支持亿、万、元、角、分等单位。注意,该方法只支持中文大写金额格式,不支持阿拉伯数字和英文格式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值