zoukankan      html  css  js  c++  java
  • 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);
                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("壹万伍仟肆佰壹拾元贰角捌分肆厘"));
            
        }

    原创。性能待优化

    优化版本如下:

        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;
        }

    减少遍历嵌套。提升性能。

  • 相关阅读:
    无向图判断割点
    C
    连通图 求至少有给几个点信息才能传遍全图,至少添加几条边才能使全图联通
    线段树区间更新(set暴力)
    A
    辗转相除法(数学推理)
    Python List index()方法
    Python List extend()方法
    Python List count()方法
    Python List append()方法
  • 原文地址:https://www.cnblogs.com/tsxylhs/p/13693019.html
Copyright © 2011-2022 走看看