hjh132's Blog
          If a thing is worth doing it is worth doing well.
          posts - 20,comments - 9,trackbacks - 0
          /**
           * 數字轉化成中文輸出,如123456789,輸出為一億二千三百四拾五萬六千七百八百九
           * 
          @author HJH
           * 
          @version 1.0,02/26/2008
           
          */

          public class ReadNumber
          {
              
          public static String readNum(String str)
              
          {
                  String temp 
          = init(str);
                  String[] strArray 
          = separate(temp);
                  String tempStr 
          = readNumArray(strArray);
                  
                  
          return tempStr;
              }

              
              
          public static String init(String str)
              
          {
                  str 
          = str.replace("1","");
                  str 
          = str.replace("2","");
                  str 
          = str.replace("3","");
                  str 
          = str.replace("4","");
                  str 
          = str.replace("5","");
                  str 
          = str.replace("6","");
                  str 
          = str.replace("7","");
                  str 
          = str.replace("8","");
                  str 
          = str.replace("9","");
                  str 
          = str.replace("0","");

                  
          return str;
              }

              
              
          public static String[] separate(String str)  //將str分成每四個一組
              {
                  
          int len = str.length();
                  
          int divInt = len/4//
                  int divFloat = len%4//余數
                  String[] strArray = new String[divInt + 1];
                  
          int count = 0;
                  
                  
          for (int i = 0; i < strArray.length; i++)
                  
          {
                      
          if (i == 0)
                          strArray[i] 
          = str.substring(0,divFloat);
                      
          else
                      
          {
                          strArray[i] 
          = str.substring(divFloat + count,divFloat + count + 4);
                          count 
          += 4;
                      }

                  }


                  
          return strArray;
              }

              
              
          public static String readNumArray(String[] str)
              
          {
                  String temp 
          = "";
                  String[] chNum 
          = new String[]{"","",""};
                  
          for(int i = 0; i < str.length; i++)
                  
          {
                      
          if (str.length <= 1)
                          temp 
          += readFourNum(str[i]);
                      
          else if(str.length > 1 && str.length <= 2)
                      
          {
                          temp 
          += readFourNum(str[i]) + chNum[i+1];
                      }

                      
          else if(str.length > 2 && str.length <= 3)
                          temp 
          += readFourNum(str[i]) + chNum[i];
                      
          else 
                          
          return "ERROR";
                  }

                  
          return temp;
              }

              
              
          public static String readFourNum(String str)
              
          {
                  
          int len = str.length();
                  String[] chNum 
          = new String[]{"","","",""};
                  String temp 
          = "";
                  
          switch(len)
                  
          {
                  
          case 4:
                      
          for(int i = 0; i < len; i++)
                      
          {
                          temp 
          += str.charAt(i)+  chNum[i];
                          
          //System.out.print(temp);
                      }

                      
          break;
                      
                  
          case 3:
                      
          for(int i = 0; i < len; i++)
                      
          {
                          temp 
          += str.charAt(i) + chNum[i + 1];
                      }

                      
          break;
                  
          case 2:
                      
          for(int i = 0; i < len; i++)
                      
          {
                          temp 
          += str.charAt(i) + chNum[i + 2];
                      }

                      
          break;
                  
          case 1:
                      
          for(int i = 0; i < len; i++)
                      
          {
                          temp 
          += str.charAt(i) + chNum[i + 3];
                      }

                      
          break;
                      
                  }

                  
                  
          return temp;
              }

              
              
          public static void main(String[] args)
              
          {
                  String s 
          = readNum("1234567890");
                  System.out.println(s);
              }

          }
          posted on 2008-02-26 14:18 101℃太陽 閱讀(296) 評論(1)  編輯  收藏 所屬分類: 代碼民工

          FeedBack:
          # re: 數字轉化成中文
          2008-02-29 13:10 | ~!@#$%^&*()_+
          昨天我面試也遇到這個的問題,早點看到這文章就好了~~嗚嗚~~  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 吉木萨尔县| 隆回县| 万盛区| 巩留县| 东源县| 江华| 青川县| 新和县| 江阴市| 普定县| 探索| 姜堰市| 乌拉特后旗| 乳山市| 东平县| 呼玛县| 南京市| 巴林右旗| 忻州市| 吴桥县| 弥渡县| 黎川县| 土默特右旗| 石嘴山市| 黔西县| 海伦市| 肃宁县| 都江堰市| 芜湖县| 九寨沟县| 潼南县| 滦平县| 旌德县| 都匀市| 航空| 探索| 克山县| 嵊州市| 获嘉县| 陇川县| 邻水|