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

在金融行业中经常需要把大写金额转成小写金额,之前在一次开发中有个类似的需求,翻阅了好多博文,都没找到合适的,故没办法,就花了点时间研究并实现!

实现代码如下:

java 复制代码
    private static final Character ZERO = '零';
    private static final String DIME = "角";
    private static final String CENT = "分";
    private static final String DOLLAR = "元";
    private static final String THOUSAND = "万";
    private static final String HUNDRED_MILLION = "亿";

    private static final List<Character> unitList = Arrays.asList('圆', '元', '拾', '佰', '仟', '万', '亿');

    static Map<Char, Integer> numberMap = new HashMap(10) {{
        put('零', 0);
        put('壹', 1);
        put('贰', 2);
        put('叁', 3);
        put('肆', 4);
        put('伍', 5);
        put('陆', 6);
        put('柒', 7);
        put('捌', 8);
        put('玖', 9);
    }};

    static Map<Char, Long> unitZeroMap = new HashMap(7) {{
        put('亿', 100000000L);
        put('万', 10000L);
        put('仟', 1000L);
        put('佰', 100L);
        put('拾', 10L);
        put('元', 1L);
        put('圆', 1L);
    }};

    /**
     * 把大写金额转换为单位为分的数字
     */
    public static long convertMoneyNum(String money) {
        // 按亿/万/元(圆)/角/分拆分大写金额
        List<String> moneyList = buildMoneyList(money);
        long moneyNum = 0;
        long centMoneyNum = 0;
        for (String moneyStr : moneyList) {
            if (moneyStr.endsWith(DIME)) {
                centMoneyNum += (numberMap.get(moneyStr.charAt(0)) * 10);  // 大写角转数字分
            } else if (moneyStr.endsWith(CENT)) {
                centMoneyNum += numberMap.get(moneyStr.charAt(0));    // 大写分转数字分
            } else {
                moneyNum += convertNum(moneyStr);  // 大写亿/万/元(圆)转数字元
            }
        }
        // 返回计算后的数字分
        return moneyNum * 100 + centMoneyNum;
    }


    private static long convertNum(String moneyStr) {
        char[] chars = moneyStr.toCharArray();
        Set<Char> unitZeroList = unitZeroMap.keySet();
        long moneyNumber = 0;
        int indexNum = 0;
        int maxIndex = 0;
        Character preUnit = null;
        for (int i=0; i<chars.length; i++) {
            char currNum = chars[i];
            // 处理字符零
            if (currNum == ZERO) {
                preUnit = null;
                continue;
            }
            if (unitZeroList.contains(currNum)) {  // 处理字符为单位
                long unitNum = unitZeroMap.get(currNum);
                if (preUnit != null) {
                    moneyNumber = moneyNumber * unitNum;
                    preUnit = null;
                } else {
                    int currIndex = unitList.indexOf(currNum);
                    if (currIndex > maxIndex) {
                        maxIndex = currIndex;
                        moneyNumber = (moneyNumber + indexNum) * unitNum;
                    } else {
                        moneyNumber = moneyNumber + indexNum * unitNum;
                    }
                    preUnit = currNum;
                }
            } else {
                indexNum = numberMap.get(currNum);  // 处理字符为数字
                if (i == chars.length-1) {  // 最后一个字符为数字
                    moneyNumber = moneyNumber + indexNum;
                }
                preUnit = null;
            }
        }
        return moneyNumber;
    }

   private static List<String> buildMoneyList(String money) {
        List<String> moneyList = new ArrayList<>();
        int startIndex = 0;
        // 亿
        if (money.contains(HUNDRED_MILLION)) {
            startIndex = buildMoneyList(HUNDRED_MILLION, moneyList, money, startIndex);
        }
        // 万
        if (money.contains(THOUSAND)) {
            startIndex = buildMoneyList(THOUSAND, moneyList, money, startIndex);
        }
        // 元
        if (money.contains(DOLLAR)) {
            startIndex = buildMoneyList(DOLLAR, moneyList, money, startIndex);
        }
        // 角
        if (money.contains(DIME)) {
            startIndex = buildMoneyList(DIME, moneyList, money, startIndex);
        }
        // 分
        if (startIndex < money.length()) {
            moneyList.add(money.substring(startIndex));
        }
        return moneyList;
    }


    private static int buildMoneyList(String unit, List<String> moneyList, String money, int startIndex) {
        int endIndex = money.indexOf(unit) + 1;
        moneyList.add(money.substring(startIndex, endIndex));
        return endIndex;
    }

已有好多年没写过博文了,今天记录哈!!!!

相关推荐
SenChien18 小时前
Java大模型应用开发day06-天机ai-学习笔记
java·spring boot·笔记·学习·大模型应用开发·springai
AI_567818 小时前
Airflow“3分钟上手”教程:用Python定义定时数据清洗任务
开发语言·人工智能·python
大只鹅18 小时前
Stream使用
java·开发语言
Ulyanov18 小时前
PyVista三维战场仿真实战
开发语言·python·tkinter·pyvista·gui开发
青衫码上行18 小时前
maven依赖管理和生命周期
java·学习·maven
散峰而望18 小时前
OJ 题目的做题模式和相关报错情况
java·c语言·数据结构·c++·vscode·算法·visual studio code
长安城没有风18 小时前
Java 高并发核心编程 ----- 初识多线程(上)
java·juc
董世昌4118 小时前
HTTP协议中,GET和POST有什么区别?分别适用什么场景?
java·开发语言·前端
独自破碎E18 小时前
Java中HashMap的默认负载因子为什么设置为0.75?
java·开发语言·网络
疋瓞18 小时前
C/C++查缺补漏《5》_智能指针、C和C++中的数组、指针、函数对比、C和C++中内存分配概览
java·c语言·c++