Java 基本數(shù)據(jù)類型

一、截尾和舍取

對(duì)于float傳int,什么時(shí)候會(huì)舍棄小數(shù),什么時(shí)候會(huì)將小數(shù)進(jìn)位?

如何使用代碼,從而達(dá)到準(zhǔn)確控制舍棄與進(jìn)位呢?

答案:使用函數(shù):Math.round(float f);

如下code:

public class HelloWorld {
? public static void main(String[] args) {
? ? System.out.println("My Logic keyBoard!");
? ? float above = 0.6f;
? ? float below = 0.4f;
? ? System.out.println("f2i above: "+(int)above+", below: "+(int)below);
? ? System.out.println("f2i Math.Rund above: "
? ? ? ? +Math.round(above)+", below: "+Math.round(below));
? }
}

輸出如下:

My Logic keyBoard!
f2i above: 0, below: 0
f2i Math.Rund above: 1, below: 0

總結(jié)

從上面可以看出,float傳int會(huì)有精度的丟失;此外,小類型和大類型運(yùn)算時(shí),結(jié)果位大類型。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容