[Java] java中文数字转换成阿拉伯数字 →→→→→进入此内容的聊天室

来自 , 2019-06-21, 写在 Java, 查看 132 次.
URL http://www.code666.cn/view/951124d4
  1. package com.webssky.jcseg.filter;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /**
  7.  * a class to deal with Chinese numeric. <br />
  8.  *
  9.  * @author chenxin
  10.  * {@link http://www.webssky.com}
  11.  */
  12. public class CNNMFilter {
  13.         /**
  14.          * chinese numeric chars. <br />
  15.          * i have put the chars into the lexicon file lex-cn-numeric.lex for the old version. <r />
  16.          * it's better to follow the current work.
  17.          */
  18.         private static final Character[] CN_NUMERIC = {
  19.                         '一', '二', '三', '四', '五','六', '七', '八', '九',
  20.                         '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌','玖',
  21.                         '十', '百', '千', '拾', '佰', '仟', '万', '亿', '○', 'O', '零'};
  22.        
  23.         private static Map<Character, Integer> cnNumeric = null;
  24.        
  25.         static {
  26.                 cnNumeric = new HashMap<Character, Integer>(40, 0.85f);
  27.                 for ( int j = 0; j < 9; j++ )
  28.                         cnNumeric.put(CN_NUMERIC[j], j + 1);
  29.                 for ( int j = 9; j < 18; j++ )
  30.                         cnNumeric.put(CN_NUMERIC[j], j - 8);
  31.                 cnNumeric.put('两', 2);
  32.                 cnNumeric.put('十', 10);
  33.                 cnNumeric.put('拾', 10);
  34.                 cnNumeric.put('百', 100);
  35.                 cnNumeric.put('佰', 100);
  36.                 cnNumeric.put('千', 1000);
  37.                 cnNumeric.put('仟', 1000);
  38.                 cnNumeric.put('万', 10000);
  39.                 cnNumeric.put('亿', 100000000);
  40.         }
  41.  
  42.  
  43.                 /**
  44.          * check the given char is chinese numeric or not. <br />
  45.          *
  46.          * @param c <br />
  47.          * @return boolean true yes and false for not.
  48.          */
  49.         public static int isCNNumeric( char c ) {
  50.                 Integer i = cnNumeric.get(c);
  51.                 if ( i == null ) return -1;
  52.                 return i.intValue();
  53.         }
  54.        
  55.        
  56.         /**
  57.          * a static method to turn the Chinese numeric to Arabic numbers.
  58.          *
  59.          * @param cnn
  60.          * @param boolea flag
  61.          * @return int
  62.          */
  63.         public static int cnNumericToArabic( String cnn, boolean flag ) {
  64.  
  65.                 cnn = cnn.trim();
  66.                 if ( cnn.length() == 1 )
  67.                         return isCNNumeric(cnn.charAt(0));
  68.                
  69.                 if ( flag ) cnn = cnn.replace('佰', '百')
  70.                                 .replace('仟', '千').replace('拾', '十').replace('零', ' ');
  71.                 //System.out.println(cnn);
  72.                 int yi = -1, wan = -1, qian = -1, bai = -1, shi = -1;
  73.                 int val = 0;
  74.                 yi = cnn.lastIndexOf('亿');
  75.                 if ( yi > -1 ) {
  76.                         val += cnNumericToArabic( cnn.substring(0, yi), false ) * 100000000;
  77.                         if ( yi < cnn.length() - 1 )
  78.                                 cnn = cnn.substring(yi + 1, cnn.length());
  79.                         else
  80.                                 cnn = "";
  81.                        
  82.                         if ( cnn.length() == 1 ) {
  83.                                 int arbic = isCNNumeric(cnn.charAt(0));
  84.                                 if ( arbic <= 10 )
  85.                                         val += arbic * 10000000;
  86.                                 cnn = "";
  87.                         }
  88.                 }
  89.                
  90.                 wan = cnn.lastIndexOf('万');
  91.                 if ( wan > -1 ) {
  92.                         val += cnNumericToArabic( cnn.substring(0, wan), false ) * 10000;
  93.                         if ( wan < cnn.length() - 1 )
  94.                                 cnn = cnn.substring(wan + 1, cnn.length());
  95.                         else
  96.                                 cnn = "";
  97.                         if ( cnn.length() == 1 ) {
  98.                                 int arbic = isCNNumeric(cnn.charAt(0));
  99.                                 if ( arbic <= 10 )
  100.                                         val += arbic * 1000;
  101.                                 cnn = "";
  102.                         }
  103.                 }
  104.                
  105.                 qian = cnn.lastIndexOf('千');
  106.                 if ( qian > -1 ) {
  107.                         val +=  cnNumericToArabic( cnn.substring(0, qian), false ) * 1000;
  108.                         if ( qian < cnn.length() - 1 )
  109.                                 cnn = cnn.substring(qian + 1, cnn.length());
  110.                         else
  111.                                 cnn = "";
  112.                         if ( cnn.length() == 1 ) {
  113.                                 int arbic = isCNNumeric(cnn.charAt(0));
  114.                                 if ( arbic <= 10 )
  115.                                         val += arbic * 100;
  116.                                 cnn = "";
  117.                         }
  118.                 }
  119.                
  120.                 bai = cnn.lastIndexOf('百');
  121.                 if ( bai > -1 ) {
  122.                         val += cnNumericToArabic( cnn.substring(0, bai), false ) * 100;
  123.                         if ( bai < cnn.length() - 1 )
  124.                                 cnn = cnn.substring(bai + 1, cnn.length());
  125.                         else
  126.                                 cnn = "";
  127.                         if ( cnn.length() == 1 ) {
  128.                                 int arbic = isCNNumeric(cnn.charAt(0));
  129.                                 if ( arbic <= 10 )
  130.                                         val += arbic * 10;
  131.                                 cnn = "";
  132.                         }
  133.                 }
  134.                
  135.                 shi = cnn.lastIndexOf('十');
  136.                 if ( shi > -1 ) {
  137.                         if ( shi == 0 )
  138.                                 val += 1 * 10;
  139.                         else
  140.                                 val += cnNumericToArabic( cnn.substring(0, shi), false ) * 10;
  141.                         if ( shi < cnn.length() - 1 )
  142.                                 cnn = cnn.substring(shi + 1, cnn.length());
  143.                         else
  144.                                 cnn = "";
  145.                 }
  146.                
  147.                 cnn = cnn.trim();
  148.                 for ( int j = 0; j < cnn.length(); j++ )
  149.                         val += isCNNumeric(cnn.charAt(j))
  150.                                 * Math.pow(10, cnn.length() - j - 1);
  151.                        
  152.                
  153.                 return val;
  154.         }
  155.        
  156.         public static int qCNNumericToArabic( String cnn ) {
  157.                 int val = 0;
  158.                 cnn = cnn.trim();
  159.                 for ( int j = 0; j < cnn.length(); j++ )
  160.                         val += isCNNumeric(cnn.charAt(j))
  161.                                 * Math.pow(10, cnn.length() - j - 1);
  162.                 return val;
  163.         }
  164.        
  165.         /*      public static void main(String[] args) {
  166.                 ADictionary.isCNNumeric('一');
  167.                 int val = 0;
  168.                 long s = System.nanoTime();
  169.                 //val = cnNumericToArabic("三亿二千零六万七千五百六", true);
  170.                 //val = cnNumericToArabic("一九九八", true);
  171.                 long e = System.nanoTime();
  172.                 System.out.format("Done["+val+"], cost: %.5fsec\n", ((float)(e - s)) / 1E9);
  173.         }*/
  174.        
  175. }
  176. //java/5577

回复 "java中文数字转换成阿拉伯数字"

这儿你可以回复上面这条便签

captcha