本文介紹了將 double 轉換為 Int,向下舍入的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何將 double
值轉換為 int
執行以下操作:
How to convert a double
value to int
doing the following:
Double If x = 4.97542. Convert to int x = 4.
Double If x = 4.23544. Convert to int x = 4.
也就是說,答案總是向下取整.
That is, the answer is always rounding down.
推薦答案
如果顯式cast double
為int
,小數部分將被截斷.例如:
If you explicitly cast double
to int
, the decimal part will be truncated. For example:
int x = (int) 4.97542; //gives 4 only
int x = (int) 4.23544; //gives 4 only
此外,您還可以使用 Math.floor()
方法對值進行四舍五入,以防您希望返回 double
值.
Moreover, you may also use Math.floor()
method to round values in case you want double
value in return.
這篇關于將 double 轉換為 Int,向下舍入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!