導航:首頁 > 編程語言 > javamathceil

javamathceil

發布時間:2025-06-04 07:34:52

A. Java中的Math.round()和Math.ceil(),Math.floor()區別

ceil方法:
static double ceil(double a)
返回值為double類型,返回一個大於或等於參數a的最小整數。即它返回一個整數,這個整數是所有大於等於a的整數中最小的一個。

floor方法:
static double floor(double a)
返回值為double類型,返回一個小於或等於參數a的最大整數。即它返回一個整數,這個整數是所有小於等於a的整數中最大的一個。

round方法:
//該方法為重載方法
static long round(double a)
static int round(float a)
返回最接近參數a的整數,該方法等同於Math.floor(a + 0.5)並將結果轉換為long或int類型,即四捨五入取整。

public class MathTest {
public static void main(String[] args) {
double[] nums = {-0.6, -1.5, -1, 0.5, 1.2, 1.8};
for(double n : nums) {
test(n);
}
}

public static void test(double a) {
System.out.println("Math.ceil(" + a + ")=" + Math.ceil(a));
System.out.println("Math.floor(" + a + ")=" + Math.floor(a));
System.out.println("Math.round(" + a + ")=" + Math.round(a));
}
}

運行結果:
Math.ceil(-0.6)=-0.0
Math.floor(-0.6)=-1.0
Math.round(-0.6)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.0)=-1.0
Math.floor(-1.0)=-1.0
Math.round(-1.0)=-1
Math.ceil(0.5)=1.0
Math.floor(0.5)=0.0
Math.round(0.5)=1
Math.ceil(1.2)=2.0
Math.floor(1.2)=1.0
Math.round(1.2)=1
Math.ceil(1.8)=2.0
Math.floor(1.8)=1.0
Math.round(1.8)=2

閱讀全文

與javamathceil相關的資料

熱點內容
golang演算法圖 瀏覽:698
ARM單片機開發工具 瀏覽:84
linux安裝dhcp伺服器 瀏覽:260
安卓tracker伺服器地址 瀏覽:873
寵物運輸競價源碼 瀏覽:617
阜陽前端程序員招聘 瀏覽:906
android省市聯動菜單 瀏覽:830
解壓韓國臉部spa 瀏覽:260
linuxtomcat許可權設置 瀏覽:848
法律書pdf 瀏覽:342
如何做到小愛音箱執行命令不應答 瀏覽:245
linux格式化時間 瀏覽:621
上位機單片機ds18b20 瀏覽:2
德耐爾渦旋壓縮機 瀏覽:328
pdf圖片怎麼ps 瀏覽:63
需下載什麼APP看社保信息 瀏覽:888
超高壓縮文件怎麼解壓 瀏覽:915
西門子研究生應屆程序員月薪多少 瀏覽:584
51單片機視頻下載 瀏覽:995
龍法pdf 瀏覽:748