欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于java四舍五入方法的基礎(chǔ)學(xué)習(xí)

 更新時(shí)間:2023年07月05日 09:24:52   作者:杰一定要成功!  
這篇文章主要給大家介紹了關(guān)于java四舍五入方法的基礎(chǔ)學(xué)習(xí),這是最近做算法題的時(shí)候碰到的這個(gè)問題,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

以下均為自己的學(xué)習(xí)復(fù)習(xí)資料,如有不對(duì)的地方請(qǐng)發(fā)在評(píng)論區(qū)中,我會(huì)仔細(xì)思考并作出修改,大家共同學(xué)習(xí),謝謝!

主要資料可以從java.base (Java SE 17 & JDK 17) (oracle.com)(java文檔)中查找到

四舍五入分為三種:向上取整、向下取整、四舍五入。

先給實(shí)例再談理解:

向上取整

public static double ceil(double a)

??注意 無論是輸入值和輸出值都是double類型

System.out.println(Math.ceil(10)); // 10.0
System.out.println(Math.ceil(10.1)); // 11.0
System.out.println(Math.ceil(-10.1)); // -10.0
System.out.println(Math.ceil(-10.9));//-10.0

理解:ceil可以翻譯為“天花板”。從例子可以看到,向上取整即為取該數(shù)的天花板,無論正負(fù)都取比此數(shù)大的整數(shù)即可。

向下取整

public static double floor(double a)

??注意 無論是輸入值和輸出值都是double類型

System.out.println(Math.floor(10)); // 10.0
System.out.println(Math.floor(10.9)); // 10.0
System.out.println(Math.floor(-10.1)); // -11.0
System.out.println(Math.floor(-10.9)); // -11.0

理解:floor可以翻譯為“地面”。從例子可以看到,向下取整即為取該數(shù)的地面,無論正負(fù)都取比此數(shù)小的整數(shù)即可。

四舍五入

public static long round(double a)

public static int round(float a)

??注意 當(dāng)輸入為double類型時(shí),返回為long類型;

當(dāng)輸入為float類型時(shí),返回為int類型。

System.out.println(Math.round(10.4)); // 10.0
System.out.println(Math.round(10.5)); // 11.0
System.out.println(Math.round(-10.4)); // -10.0
System.out.println(Math.round(-10.5)); // -10.0
System.out.println(Math.round(-10.6)); // -11.0

理解:round可以翻譯為“大約”。從例子可以看到,正負(fù)有兩種不同的規(guī)則

A.數(shù)為正數(shù)時(shí),遵循正常的四舍五入規(guī)則

B.數(shù)為負(fù)數(shù)時(shí),小數(shù)到0.5時(shí)還是為向上取整,只有到0.6時(shí)才會(huì)向下取整

總結(jié)

到此這篇關(guān)于java四舍五入方法的基礎(chǔ)學(xué)習(xí)的文章就介紹到這了,更多相關(guān)java四舍五入方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論