汉字数字转阿拉伯数字 和 阿拉伯数字+千/百/万/亿 转阿拉伯数字

感谢这位辛苦的大佬: http://blog.csdn.net/jjfly999/article/details/51052492#insertcode


项目的需要,把中文的汉字转成纯阿拉伯数字

比如 三千-->3000

 但是有的时候可能会输入3千,这样处理就比较麻烦一点

在网上 找了一篇文章(上面的链接),当有大数字的时候,效果不是特别好,所以进行了改进

改进后还可以识别一些变态的写法,比如:100千,300十,30千万,等等

上代码,一共3个java文件:

package com.test;

import java.math.BigInteger;  
  
public class ChineseChangeToNumber {    
    public BigInteger ChineseToNumber(String str){    
          
          
          
        String str1 = new String();    
        String str2 = new String();    
        String str3 = new String();    
          
          
        int k = 0;    
        boolean dealflag = true;    
        for(int i=0;i<str.length();i++){//先把字符串中的“零”除去    
                if('零' == (str.charAt(i))){    
                    str = str.substring(0, i) + str.substring(i+1);    
                }    
        }   
          
        String chineseNum = str;    
        for(int i=0;i<chineseNum.length();i++){    
            if(chineseNum.charAt(i) == '亿'){    
                str1 = chineseNum.substring(0,i);//截取亿前面的数字,逐个对照表格,然后转换    
                k = i+1;    
                dealflag = false;//已经处理    
                  
                if(!chineseNum.contains("万") && chineseNum.length()>i) {  
                     str3 = str.substring(i+1,chineseNum.length());   
                }  
            }    
            if(chineseNum.charAt(i) == '万'){    
                str2 = chineseNum.substring(k,i);    
                str3 = str.substring(i+1);    
                dealflag = false;//已经处理    
            }   
           
        }    
        if(dealflag){//如果没有处理    
            str3 =  chineseNum;    
        }    
          
        BigInteger str1Int=sectionChinese(str1).multiply(new BigInteger("100000000"));  
        BigInteger str2Int=sectionChinese(str2). multiply(new BigInteger("10000"));  
        BigInteger str3Int=sectionChinese(str3);    
          
        BigInteger result =str1Int.add(str2Int).add(str3Int);  
                 
        return result;    
    }    
        
    public BigInteger sectionChinese(String str){    
  
        int value = 0;  
          
        int sectionNum = 0;    
        for(int i=0;i<str.length();i++){    
          
            int v = (int) Tool.intList.get(str.charAt(i));  
            if(v>=0 && v<=9) {  
                if(sectionNum==0) {  
                    sectionNum=v;  
                }else {  
                    sectionNum =sectionNum*10+ v;  
                }  
                  
                if(i == str.length()-1) {  
                    value=sectionNum+value;  
                }  
            }  
              
            if(v == 10 || v == 100 || v == 1000) {  
                if(i==0) {  
                    sectionNum=1;  
                }  
                sectionNum=sectionNum*v;  
                value=sectionNum+value;  
                sectionNum=0;  
            }  
        }    
        return new BigInteger(String.valueOf(value));
    }		
   }




package com.test;


import java.util.HashMap;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;    
    
public class Tool {    
    //数字位    
    public static String[] chnNumChar = {"零","一","二","三","四","五","六","七","八","九"};    
    public static char[] chnNumChinese = {'零','一','二','三','四','五','六','七','八','九'};    
    //节权位    
    public static String[] chnUnitSection = {"","万","亿","万亿"};      
    //权位    
    public static String[] chnUnitChar = {"","十","百","千"};    
    public static HashMap intList = new HashMap();    
    static{    
        for(int i=0;i<chnNumChar.length;i++){    
            intList.put(chnNumChinese[i], i);    
        }    
          
        for(char i=0;i<100;i++){    
            intList.put(new Character((char)(48+i)), new Integer(i));    
        }   
        
          
        intList.put('十',10);    
        intList.put('百',100);    
        intList.put('千', 1000);    
         
    }   
}    

下面的是测试类 :


package com.test;
public class Main {    
    public static void main(String[] args) {    
        Main ma = new Main();    
        Tool to = new Tool();    
        ma.initMain();    
    }    
    public void initMain(){    
        testChineseToNumber();    
    }    
    public void testChineseToNumber(){    
        ChineseChangeToNumber chineseToNumber = new ChineseChangeToNumber();    
          
        String a[]= {  "1","22","333","444",  
                       "三","六","八",  
                       "二十","八十",  
                       "一百","三百",  
                       "一千",  
                       "一万","十万","三十万","二十万","三百万",  
                       "一亿","十亿","二十亿","四百亿","四千亿",  
                       "二十三","四十五","三十六",  
                       "三百二十","六百三十","八百九十",  
                       "三千六百二十","五千七百三十",  
                       "一万二千二百三十六","十万八千六百三十四","二十万三千","二十万零三千","一百万零八千零六十","七百万零五十六","四百万","一千万零八百六十四","八千万零九百八十二",  
                       "一亿零八十","一亿八千六百三十二万三千六百五十二","十亿六千零五十四万三千","九亿零八百六十二", //1,  
                       "三百亿","三百亿零四百八十万三千六百二十","三百亿零三十","三百亿三千万","三百亿零三万",  
                         
                       "3千","4万","4十",  
                       "1千万","3千万","50千万",  
                       "3000万","3456万","34561万",  
                       "3000万800","3亿800万",  
                         
                       "3亿80万88","200亿40万123",  
                         
                       "30千","300十","40十万","40百万",  
                       "30千20","50千4","300十5","40百万80",  
        };  
          
        for(int i=0;i<a.length;i++) {  
             System.out.println(a[i]+"--->"+chineseToNumber.ChineseToNumber(a[i]));  
        }  
    }    
}    



改进后的 源码  ,来看一下 测试的结果:

1--->1
22--->22
333--->333
444--->444
三--->3
六--->6
八--->8
二十--->20
八十--->80
一百--->100
三百--->300
一千--->1000
一万--->10000
十万--->100000
三十万--->300000
二十万--->200000
三百万--->3000000
一亿--->100000000
十亿--->1000000000
二十亿--->2000000000
四百亿--->40000000000
四千亿--->400000000000
二十三--->23
四十五--->45
三十六--->36
三百二十--->320
六百三十--->630
八百九十--->890
三千六百二十--->3620
五千七百三十--->5730
一万二千二百三十六--->12236
十万八千六百三十四--->108634
二十万三千--->203000
二十万零三千--->203000
一百万零八千零六十--->1008060
七百万零五十六--->7000056
四百万--->4000000
一千万零八百六十四--->10000864
八千万零九百八十二--->80000982
一亿零八十--->100000080
一亿八千六百三十二万三千六百五十二--->186323652
十亿六千零五十四万三千--->1060543000
九亿零八百六十二--->900000862
三百亿--->30000000000
三百亿零四百八十万三千六百二十--->30004803620
三百亿零三十--->30000000030
三百亿三千万--->30030000000
三百亿零三万--->30000030000
3千--->3000
4万--->40000
4十--->40
1千万--->10000000
3千万--->30000000
50千万--->500000000
3000万--->30000000
3456万--->34560000
34561万--->345610000
3000万800--->30000800
3亿800万--->308000000
3亿80万88--->300800088
200亿40万123--->20000400123
30千--->30000
300十--->3000
40十万--->4000000
40百万--->40000000
30千20--->30020
50千4--->50004
300十5--->3005
40百万80--->40000080

转换的效果还是可以的.




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值