- package com.pyc.maven.demo.jerry;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- /**
- *
- * @author boy
- *
- */
- public class Analysis {
- private final static Map<Character,Integer> unitMap = new HashMap<Character,Integer>();
- private final static Map<Character,Integer> amountMap = new HashMap<Character,Integer>();
- static{
- unitMap.put('亿', 100000000);
- unitMap.put('万', 10000);
- unitMap.put('千', 1000);
- unitMap.put('佰', 100);
- unitMap.put('拾', 10);
- unitMap.put('圆', 1);
- amountMap.put('玖', 9);
- amountMap.put('捌', 8);
- amountMap.put('柒', 7);
- amountMap.put('陆', 6);
- amountMap.put('伍', 5);
- amountMap.put('肆', 4);
- amountMap.put('叁', 3);
- amountMap.put('贰', 2);
- amountMap.put('壹', 1);
- }
- public static void main(String[] args) {
- long start = System.currentTimeMillis();
- System.out.println(analysis("壹佰贰拾叁万"));
- System.out.println(analysis("玖拾玖"));
- System.out.println(analysis("壹万零叁佰"));
- System.out.println(analysis("壹万零叁"));
- System.out.println(analysis("叁千万"));
- long end = System.currentTimeMillis();
- System.err.println(end - start);
- }
- /**
- * 大写的人民币金额转换为数字
- * @param target
- * @return
- */
- public static int analysis(String target){
- if(null == target || "".equals(target))
- return 0;
- if(!amountMap.containsKey(target.charAt(0))){
- target = "壹"+target;
- }
- if(!unitMap.containsKey(target.charAt(target.length()-1))){
- target = target+"圆";
- }
- char[] cc = target.toCharArray();
- System.out.println(Arrays.toString(cc));
- System.out.println("************************************");
- /**
- * 中间变量
- */
- int temp = 0;
- /**
- * 之前取到的单位的值
- */
- int preUnitValue = 0;
- /**
- * 结果
- */
- int result = 0;
- for(char c : cc){
- /**
- * 拿到数字
- */
- if(isAmount(c)){
- temp = amountMap.get(c);
- }
- /**
- * 拿到单位
- */
- else if(isUnit(c)){
- int unitValue = unitMap.get(c);
- /**
- * 单位<=单位模式,相乘
- * 单位>单位模式,相加
- */
- if(preUnitValue !=0 && unitValue >= preUnitValue){
- result = (result+temp) * unitValue;
- }else{
- result = result + temp*unitValue;
- }
- preUnitValue = unitValue;
- temp = 0;
- }
- }
- return result;
- }
- /**
- * 判断是否是数字
- * @param c
- * @return
- */
- private static boolean isAmount(char c){
- return amountMap.containsKey(c);
- }
- /**
- * 判断是否是单位
- * @param c
- * @return
- */
- private static boolean isUnit(char c){
- return unitMap.containsKey(c);
- }
- }
人名币大写转小写
最新推荐文章于 2024-11-11 00:03:55 发布