首页 > 编程语言 > 详细

Java 大写金额转换成数字

时间:2020-09-18 22:59:17      阅读:77      评论:0      收藏:0      [点我收藏+]
  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);
            for (Map.Entry<Character, Double> entry : map.entrySet()) { //遍历数字map
                boolean flag = false;
                if (c == entry.getKey()) {
                    if (temp != -1) {
                        result += temp;
                        temp = -1;
                    }
                    temp = entry.getValue();
                    break;
                } else {
                    if (temp == -1) {
                        continue;
                    }
                    for (Map.Entry<Character, Double> entry1 : map1.entrySet()) {//遍历单位map
                        if (c == entry1.getKey()) {
                            temp *= entry1.getValue();
                            flag = true;
                            break;
                        }
                    }
                }
                if (flag) {
                    break;
                }
            }
            if (i == amount.length() - 1) {
                result += temp;
            }
        }
        return result;
    }

测试Demo

    public static void main(String[] args) {
        CNYtoNum c = new CNYtoNum();
        System.out.println(c.chineseNumber2Int("壹万伍仟肆佰壹拾元贰角捌分肆厘"));
        
    }

原创。性能待优化

Java 大写金额转换成数字

原文:https://www.cnblogs.com/tsxylhs/p/13693019.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!