将中文数字转为数字

Posted 日月心诚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将中文数字转为数字相关的知识,希望对你有一定的参考价值。

  1     /**
  2       * 根据输入的中文转为数字
  3       * @param String chineseNumber
  4       * @return int
  5       */
  6      public static int chineseNumber2Int(String chineseNumber){
  7          String state=chineseNumber;
  8          int result=0;
  9          
 10          if(state=="" || state==null){
 11             try {
 12                 throw new Exception("输入不合法!");
 13             } catch (Exception e) {
 14                 // TODO Auto-generated catch block
 15                 e.printStackTrace();
 16             }
 17          }
 18          else if(state.contains("千")||state.contains("百") || state.contains("十")){
 19             int temp = 1;//存放一个单位的数字如:十万
 20             int flag = 0;//判断是否有中文数字的单位
 21             char[] cnArr = new char[]{‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘,‘七‘,‘八‘,‘九‘};
 22              char[] chArr = new char[]{‘十‘,‘百‘,‘千‘,‘万‘,‘亿‘};
 23             //遍历输入的中文
 24             for (int i = 0; i < state.length(); i++) {
 25                 boolean isCHUnit = true;//判断是否是chArr
 26                 char c = state.charAt(i);//字符
 27                 //判断是否数字
 28                 for (int j = 0; j < cnArr.length; j++) {//非单位,即数字
 29                     if (c == cnArr[j]) {
 30                         if(0 != flag){
 31                             //添加下一个单位之前,先把上一个单位值添加到结果中
 32                             result += temp;
 33                             //临时值重置
 34                             temp = 1;
 35                             flag = 0;
 36                         }
 37                         // 下标+1,就是对应的值
 38                         temp = j + 1;
 39                         isCHUnit = false;
 40                         break;
 41                     }
 42                 }
 43                 //判断是否单位
 44                 if(isCHUnit){//单位{‘十‘,‘百‘,‘千‘,‘万‘,‘亿‘}
 45                     for (int j = 0; j < chArr.length; j++) {
 46                         if (c == chArr[j]) {
 47                             switch (j) {
 48                             case 0:
 49                                 temp *= 10;
 50                                 break;
 51                             case 1:
 52                                 temp *= 100;
 53                                 break;
 54                             case 2:
 55                                 temp *= 1000;
 56                                 break;
 57                             case 3:
 58                                 temp *= 10000;
 59                                 break;
 60                             case 4:
 61                                 temp *= 100000000;
 62                                 break;
 63                             default:
 64                                 break;
 65                             }
 66                             flag++;
 67                         }
 68                     }
 69                 }
 70                 if (i == state.length() - 1) {//遍历到最后一个字符
 71                     result += temp;
 72                 }
 73             }
 74             }
 75          else{
 76              int temp=1;
 77              String tempS="";
 78              char[] cnArr = new char[]{‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘,‘七‘,‘八‘,‘九‘};
 79              for (int i = 0; i < state.length(); i++) {
 80                     char c = state.charAt(i);//字符
 81                     if(c==‘零‘){
 82                         temp=0;
 83                     }
 84                     else{
 85                         for (int j = 0; j < cnArr.length; j++) {//非单位,即数字
 86                             if (c == cnArr[j]) {
 87                                 temp = j + 1;
 88                                 break;
 89                             }
 90                         }    
 91                     }
 92                     
 93                     tempS+=temp;
 94                 }
 95              
 96              result=Integer.parseInt(tempS);
 97          }
 98          System.out.println(result);
 99             return result;
100         }

 

以上是关于将中文数字转为数字的主要内容,如果未能解决你的问题,请参考以下文章

Python 将中文大写数字转为阿拉伯数字

如何将人民币数字转为大写

用vue将数字转为中文大写金额

将int数字转为中文字符串

Java将数字转为汉字(中国钱币-数字转汉字)

Js将数字转化为中文大写