這篇文章主要介紹“怎么使用java數(shù)學(xué)工具類Math”,在日常操作中,相信很多人在怎么使用java數(shù)學(xué)工具類Math問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用java數(shù)學(xué)工具類Math”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
1. 概述
java.util.Math類是數(shù)學(xué)相關(guān)的工具類,里面提供了大量的靜態(tài)方法,完成與數(shù)學(xué)運算相關(guān)的操作。
2. 基本的方法
public static double abs(double num);獲取絕對值。有多種重載,absolutely絕對地public static double ceil(double num);向上取整,ceil是天花板的意思public static double floor(double num);向下取整,floor是地板的意思public static long round(double num);四舍六入五成雙(看下面代碼的注釋),round有大約,完整的意思
3. 四種方法一起通過代碼演示一遍
public class MathMethod { public static void main(String[] args) { //abs方法,取絕對值 System.out.println(Math.abs(3.14)); //3.14 System.out.println(Math.abs(0)); //0 System.out.println(Math.abs(-2.2)); //2.2 System.out.println("---------------------"); //ceil方法,向上取整,往大的靠 System.out.println(Math.ceil(3.2)); //4.0 System.out.println(Math.ceil(3.8)); //4.0 System.out.println(Math.ceil(-3.2)); //-3.0 System.out.println(Math.ceil(-3.8)); //-3.0 System.out.println("---------------------"); //floor方法,向下取整,往小的靠 System.out.println(Math.floor(3.2)); //3.0 System.out.println(Math.floor(3.8)); //3.0 System.out.println(Math.floor(-3.2)); //-4.0 System.out.println(Math.floor(-3.8)); //-4.0 System.out.println("---------------------");
//【注意,面試高頻】round方法,四舍 六入 五成雙 //先看看四舍六入,如果出現(xiàn)負數(shù),先轉(zhuǎn)成正數(shù),再四舍六入,最后加上負號 System.out.println(Math.round(3.4)); //3 System.out.println(Math.round(3.6)); //4 System.out.println(Math.round(-3.4)); //-3 System.out.println(Math.round(-3.6)); //-4 //五成雙是什么意思呢?當(dāng)出現(xiàn)0.5結(jié)尾的時候,就給它再加上+0.5,5不就成雙了 //接著再對相加的結(jié)果進行floor運算 System.out.println(Math.round(-2.5)); //-2 System.out.println(Math.floor(-2.5 + 0.5)); //與Math.round(-2.5)結(jié)果一致 System.out.println(Math.round(2.5)); //3 System.out.println(Math.floor(2.5 + 0.5)); //與Math.round(2.5)結(jié)果一致 }}
4. 圓周率Math.PI
在Math類的源碼中,我們可以看到,它自定義的圓周率 PI = 3.14159265358979323846
以后的計算如果需要用到PI,盡量用已經(jīng)定義好的圓周率,非常精確
到此,關(guān)于“怎么使用java數(shù)學(xué)工具類Math”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
名稱欄目:怎么使用java數(shù)學(xué)工具類Math-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://aaarwkj.com/article18/cdhegp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站制作、Google、做網(wǎng)站、網(wǎng)站維護、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)