共計 325 個字符,預計需要花費 1 分鐘才能閱讀完成。
在 Java 中將 double
類型轉換為 int
類型可以使用強制類型轉換或者使用 Math
類提供的方法進行轉換。
-
強制類型轉換:
double d = 3.14; int i = (int) d;
-
使用
Math
類提供的方法:- 使用
Math.floor()
方法將double
向下取整轉換為int
:double d = 3.14; int i = (int) Math.floor(d);
- 使用
Math.ceil()
方法將double
向上取整轉換為int
:double d = 3.14; int i = (int) Math.ceil(d);
- 使用
Math.round()
方法將double
四舍五入轉換為int
:double d = 3.14; int i = (int) Math.round(d);
- 使用
丸趣 TV 網 – 提供最優質的資源集合!
正文完