java中int和Integer的轉換以及四捨五入

1 String 和int之間的轉換:

1 如何將字串 String 轉換成整數 int?

A. 有兩個方法:

1). int i = Integer.parseInt([String]); 或

i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串轉成 Double, Float, Long 的方法大同小異.

2 如何將整數 int 轉換成字串 String ?

A. 有叄種方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 轉成字串的方法大同小異.

2 四色五入轉換成整數

DecimalFormat df = new DecimalFormat("######0"); //四色五入轉換成整數

df.format(x); 注意:.format返回的是String。

java中int和Integer的轉換以及四捨五入


分享到:


相關文章: