漢字の数字はアラビア数字とアラビア数字+千/百/万/億回転アラビア数字

7321 ワード

この苦労した男に感謝します.http://blog.csdn.net/jjfly999/article/details/51052492#insertcode
プロジェクトの必要性は、中国語の漢字を純粋なアラビア数字に変えることです.
例えば三千-->3000
 しかし、3千を入力する場合もあります.これでは処理が面倒になります.
ネットで1篇の文章(上のリンク)を探して、大きい数字がある时、効果は特に良くなくて、だから改善しました
改善後、100千、300十、30千万などの変態の書き方を認識することもできます.
コードを入力します.javaファイルは全部で3つあります.
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;ii) {  
                     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=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

次はテストクラスです.
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"+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

変換の効果はまあまあです.