本文介紹了如何使用 Java 將 float 轉換為 int的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我使用以下行將 float 轉換為 int,但它并不像我想要的那樣準確:
I used the following line to convert float to int, but it's not as accurate as I'd like:
float a=8.61f;
int b;
b=(int)a;
結果是:8
(應該是9
)
當a = -7.65f
時,結果為:-7
(應該是-8
)
When a = -7.65f
, the result is : -7
(It should be -8
)
最好的方法是什么?
推薦答案
使用 Math.round()
會將浮點數四舍五入到最接近的整數.
Using Math.round()
will round the float to the nearest integer.
這篇關于如何使用 Java 將 float 轉換為 int的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!