☆藍色夢想☆

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

          Java類型轉換的一個工具類

          Posted on 2006-06-08 23:07 ☆藍色夢想☆ 閱讀(1234) 評論(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");
          主站蜘蛛池模板: 环江| 婺源县| 五峰| 通渭县| 漳平市| 通山县| 澄城县| 淮南市| 平潭县| 古田县| 拜城县| 五华县| 连平县| 定远县| 南投市| 祁阳县| 垦利县| 济阳县| 海原县| 英山县| 葵青区| 蚌埠市| 都江堰市| 渝中区| 台安县| 厦门市| 临澧县| 鄂伦春自治旗| 长武县| 略阳县| 佛坪县| 鹤庆县| 牙克石市| 如东县| 凤山市| 东源县| 定南县| 阿瓦提县| 松桃| 崇州市| 信丰县|