您的位置:首页 > 财经 > 产业 > 北京自动seo_视频拍摄app_网店运营推广_推广和竞价代运营

北京自动seo_视频拍摄app_网店运营推广_推广和竞价代运营

2024/11/18 5:25:38 来源:https://blog.csdn.net/u011407281/article/details/142708856  浏览:    关键词:北京自动seo_视频拍摄app_网店运营推广_推广和竞价代运营
北京自动seo_视频拍摄app_网店运营推广_推广和竞价代运营

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

实现代码如下:

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

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com