1. 程式人生 > >Java中String字串轉整型或浮點型

Java中String字串轉整型或浮點型

 學習筆記:

 轉換為浮點型:

    使用Double或者Float的parseDouble或者parseFloat方法進行轉換

String   s   =   "123.456 ";  //要確保字串為一個數值,否則會出異常
double   d   =   Double.parseDouble(s); 
float   f   =   Float.parseFloat(s);

轉換為整型: 

  使用Integer的parseInt方法進行轉換。

int i = Integer.parseInt([String]);//[String]待轉換的字串