☆藍色夢想☆

          世界總是反反覆覆錯錯落落地飄去 來不及嘆息 生活不是平平淡淡從從容容的東西 不能放棄
          posts - 57, comments - 5, trackbacks - 0, articles - 0

          Java類型轉(zhuǎn)換的一個工具類

          Posted on 2006-06-08 23:07 ☆藍色夢想☆ 閱讀(1234) 評論(0)  編輯  收藏 所屬分類: J2SE
          我只把自己常用的數(shù)據(jù)類型互相轉(zhuǎn)換做成了一個類,大家也可以根據(jù)自己的需要把其他的各種情況中用到的轉(zhuǎn)換方法補充進來使其更實用更強大

          代碼內(nèi)容
          /*
          * Created on 2005-6-6
          * Made In GamVan
          */
          package com.gamvan.tools;
          public class TypeChange {


          public static String nullOfString(String str){
          if(str==null){
          str = "";
          }
          return str;
          }

          public static byte stringToByte(String str){
          byte b = 0;
          if(str!=null){
          try{
          b = Byte.parseByte(str);
          }catch(Exception e){

          }
          }
          return b;
          }

          public static boolean stringToBoolean(String str){
          if(str==null){
          return false;
          }else{
          if(str.equals("1")){
          return true;
          }else if(str.equals("0")){
          return false;
          }else{
          try{
          return Boolean.parseBoolean(str);
          }catch(Exception e){
          return false;
          }
          }
          }
          }

          public static int stringToInt(String str){
          int i=0;
          if(str!=null){
          try{
          i = Integer.parseInt(str.trim());
          }catch(Exception e){
          i = 0;
          }

          }else{
          i = 0;
          }
          return i;
          }
          public static short stringToShort(String str){
          short i=0;
          if(str!=null){
          try{
          i = Short.parseShort(str.trim());
          }catch(Exception e){
          i = 0;
          }
          }else{
          i = 0;
          }
          return i;
          }


          public static double stringToDouble(String str){
          double i=0;
          if(str!=null){
          try{
          i = Double.parseDouble(str.trim());
          }catch(Exception e){
          i = 0;
          }
          }else{
          i = 0;
          }
          return i;
          }

          public static String intToString(int i){
          String str = "";
          try{
          str = String.valueOf(i);
          }catch(Exception e){
          str = "";
          }
          return str;
          }


          public static long doubleToLong(double d){
          long lo=0;
          try{
          //double轉(zhuǎn)換成long前要過濾掉double類型小數(shù)點后數(shù)據(jù)
          lo = Long.parseLong(String.valueOf(d).substring(0,String.valueOf(d).lastIndexOf(".")));
          }catch(Exception e){
          lo=0;
          }
          return lo;
          }

          public static int doubleToInt(double d){
          int i=0;
          try{
          //double轉(zhuǎn)換成long前要過濾掉double類型小數(shù)點后數(shù)據(jù)
          i = Integer.parseInt(String.valueOf(d).substring(0,String.valueOf(d).lastIndexOf(".")));
          }catch(Exception e){
          i=0;
          }
          return i;
          }

          public static double longToDouble(long d){
          double lo=0;
          try{
          lo = Double.parseDouble(String.valueOf(d));
          }catch(Exception e){
          lo=0;
          }
          return lo;
          }

          public static int longToInt(long d){
          int lo=0;
          try{
          lo = Integer.parseInt(String.valueOf(d));
          }catch(Exception e){
          lo=0;
          }
          return lo;
          }
          public static long stringToLong(String str) {
          Long li = new Long(0);
          try{
          li = Long.valueOf(str);
          }catch(Exception e){
          //li = new Long(0);
          }
          return li.longValue();
          }
          public static String longToString(long li) {
          String str = "";
          try{
          str = String.valueOf(li);
          }catch(Exception e){

          }
          return str;
          }

          }

          相信大家都能看懂,這里就不解釋了,關(guān)于使用我舉個例子字符串轉(zhuǎn)換成Int類型? 如下

          代碼內(nèi)容
          int i = TypeChange.stringToInt("213324");
          主站蜘蛛池模板: 江永县| 明光市| 民乐县| 稻城县| 藁城市| 东辽县| 会昌县| 同江市| 微博| 印江| 清原| 日土县| 夏邑县| 延寿县| 炉霍县| 阜新市| 黔江区| 龙海市| 康马县| 稷山县| 白城市| 德庆县| 策勒县| 柘城县| 台江县| 客服| 陈巴尔虎旗| 晋州市| 泊头市| 吴江市| 周宁县| 鄂伦春自治旗| 南充市| 察雅县| 昌宁县| 广丰县| 响水县| 乌拉特中旗| 五大连池市| 集安市| 玛沁县|