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

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

  • 相关阅读:
    Chapter 14 高级I/O
    UNP总结 Chapter 3 套接字编程简介
    UNP总结 Chapter 1 简介
    Chapter 13 守护进程
    Chapter 17 高级进程间通信
    Chapter 15 进程间通信
    实例详解JSP内置对象
    一个记录程序运行时间表的控件
    .Grove—— .Net下的ORM框架
    .Net 下的Wondows窗体常用项目
  • 原文地址:https://www.cnblogs.com/tsxylhs/p/13693019.html
Copyright © 2020-2023  润新知