☆藍色夢想☆

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

          Java類型轉換的一個工具類

          Posted on 2006-06-08 23:07 ☆藍色夢想☆ 閱讀(1237) 評論(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");
          主站蜘蛛池模板: 拜城县| 阿巴嘎旗| 湟中县| 金山区| 胶州市| 会同县| 洛宁县| 新密市| 云南省| 南投县| 遵化市| 唐山市| 无极县| 潢川县| 奉节县| 开远市| 芒康县| 油尖旺区| 宜川县| 贵南县| 申扎县| 夏津县| 井陉县| 安多县| 桦甸市| 林芝县| 敦化市| 棋牌| 萍乡市| 承德县| 来凤县| 西乡县| 海城市| 会泽县| 鄂尔多斯市| 柳河县| 义乌市| 石渠县| 昌宁县| 富源县| 新津县|