hjh132's Blog
          If a thing is worth doing it is worth doing well.
          posts - 20,comments - 9,trackbacks - 0
          /**
           * 數(shù)字轉(zhuǎn)化成中文輸出,如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分成每四個(gè)一組
              {
                  
          int len = str.length();
                  
          int divInt = len/4//
                  int divFloat = len%4//余數(shù)
                  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℃太陽(yáng) 閱讀(293) 評(píng)論(1)  編輯  收藏 所屬分類: 代碼民工

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 黔东| 庆城县| 甘肃省| 张北县| 永善县| 济源市| 普陀区| 南部县| 图片| 平山县| 桐庐县| 宜州市| 灵山县| 德安县| 武义县| 通州区| 土默特左旗| 祁东县| 横山县| 丹江口市| 邹城市| 华坪县| 五寨县| 秦安县| 衡山县| 临泽县| 滁州市| 佛山市| 南京市| 泾阳县| 邳州市| 集贤县| 田东县| 永善县| 海晏县| 剑河县| 广汉市| 新龙县| 方城县| 闽清县| 石景山区|