Java的資料轉換
1 字串轉換成資料
字串轉換成整數:
String MyNumber="1234";
int MyInt =Integer.parseInt(MyNumber);
字串轉換成byte, short, int,float, double, long等資料類型,可
以分別參考Byte, Short,Integer, Float, Double, Long類的parseXXX
方法。
2 資料轉換成字串
整數轉換成字串:
int MyInt = 1234;
String MyString ="" + MyInt;
其它資料類型可以利用同樣的方法轉換成字串。
3 十進位到其他進位的轉換
十進位整數轉換成二進位整數,返回結果是一個字串:
Integer.toBinaryString(inti);
Integer和Long提供了toBinaryString, toHexString和toOctalString方
法,可以方便的將資料轉換成二進位、十六進位和八進位字串。功能更
加強大的是其toString(int/longi, int radix)方法,可以將一個十進 制數轉換成任意進位的字串形式。
byte, short, float和double等資料類型,可以利用Integer或者是Long 的toBinaryString,toHexString, to OctalString和toString方法轉換 成其他進位的字串形式。
4
其它進位到十進位的轉換
五進位字串14414轉換成十進位整數,結果是1234:
System.out.println(Integer.valueOf("14414",5);
Integer和Long提供的valueOf(String source, int radix)方法,可以 將任意進位的字串轉換成十進位資料。
1、字串String轉化為整數int int i = Integer.parseInt(str); int i = Integer.valueOf(my_str).intValue();注: 字串轉成Double, Float, Long的方法大同小異。 2、將字串String轉化為Integer Integer integer=Integer.valueOf(i)3、將整數 int 轉換成字串 String? 有三種方法: String s = String.valueOf(i); String s = Integer.toString(i); String s = "" + i;註:Double, Float, Long 轉成字串的方法大同小異。4、將整數int轉化為Integer Integer integer=new Integer(i)5、如何將Integer轉化為字串String Integer integer=String()6、將Integer轉化為int int num=Integer.intValue()7、將String轉化為BigDecimal BigDecimal d_id=new BigDecimal(str)