☆藍色夢想☆

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

          Java類型轉換的一個工具類

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

          代碼內容
          /*
          * 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轉換成long前要過濾掉double類型小數點后數據
          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轉換成long前要過濾掉double類型小數點后數據
          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;
          }

          }

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

          代碼內容
          int i = TypeChange.stringToInt("213324");
          主站蜘蛛池模板: 兴隆县| 马关县| 金溪县| 卓资县| 泗洪县| 承德市| 扶绥县| 安宁市| 红桥区| 桦川县| 台山市| 阳春市| 宝鸡市| 乌兰县| 长垣县| 宝丰县| 武鸣县| 长沙县| 南京市| 福安市| 鞍山市| 秦皇岛市| 鱼台县| 尼木县| 汤阴县| 昌黎县| 宁强县| 夏邑县| 慈溪市| 红原县| 义乌市| 石嘴山市| 临湘市| 天台县| 枣阳市| 麟游县| 景洪市| 手游| 河津市| 铁力市| 寿光市|