Java中Math類常用方法代碼詳解
近期用到四舍五入想到以前整理了一點,就順便重新整理好經(jīng)常見到的一些四舍五入,后續(xù)遇到常用也會直接在這篇文章更新。。。
public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//計算平方根 *Math.cbrt()//計算立方根 *Math.pow(a, b)//計算a的b次方 *Math.max( , );//計算最大值 *Math.min( , );//計算最小值 */ System.out.println(Math.sqrt(16)); //4.0 System.out.println(Math.cbrt(8)); //2.0 System.out.println(Math.pow(3,2)); //9.0 System.out.println(Math.max(2.3,4.5));//4.5 System.out.println(Math.min(2.3,4.5));//2.3 /** * abs求絕對值 */ System.out.println(Math.abs(-10.4)); //10.4 System.out.println(Math.abs(10.1)); //10.1 /** * ceil天花板的意思,就是返回大的值 */ System.out.println(Math.ceil(-10.1)); //-10.0 System.out.println(Math.ceil(10.7)); //11.0 System.out.println(Math.ceil(-0.7)); //-0.0 System.out.println(Math.ceil(0.0)); //0.0 System.out.println(Math.ceil(-0.0)); //-0.0 System.out.println(Math.ceil(-1.7)); //-1.0 /** * floor地板的意思,就是返回小的值 */ System.out.println(Math.floor(-10.1)); //-11.0 System.out.println(Math.floor(10.7)); //10.0 System.out.println(Math.floor(-0.7)); //-1.0 System.out.println(Math.floor(0.0)); //0.0 System.out.println(Math.floor(-0.0)); //-0.0 /** * random 取得一個大于或者等于0.0小于不等于1.0的隨機數(shù) */ System.out.println(Math.random()); //小于1大于0的double類型的數(shù) System.out.println(Math.random()*2);//大于0小于1的double類型的數(shù) System.out.println(Math.random()*2+1);//大于1小于2的double類型的數(shù) /** * rint 四舍五入,返回double值 * 注意.5的時候會取偶數(shù) 異常的尷尬=。= */ System.out.println(Math.rint(10.1)); //10.0 System.out.println(Math.rint(10.7)); //11.0 System.out.println(Math.rint(11.5)); //12.0 System.out.println(Math.rint(10.5)); //10.0 System.out.println(Math.rint(10.51)); //11.0 System.out.println(Math.rint(-10.5)); //-10.0 System.out.println(Math.rint(-11.5)); //-12.0 System.out.println(Math.rint(-10.51)); //-11.0 System.out.println(Math.rint(-10.6)); //-11.0 System.out.println(Math.rint(-10.2)); //-10.0 /** * round 四舍五入,float時返回int值,double時返回long值 */ System.out.println(Math.round(10.1)); //10 System.out.println(Math.round(10.7)); //11 System.out.println(Math.round(10.5)); //11 System.out.println(Math.round(10.51)); //11 System.out.println(Math.round(-10.5)); //-10 System.out.println(Math.round(-10.51)); //-11 System.out.println(Math.round(-10.6)); //-11 System.out.println(Math.round(-10.2)); //-10 } }
總結(jié)
以上所述是小編給大家介紹的Java中Math類常用方法代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留
言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
JDBC連接SQL?Server數(shù)據(jù)庫實現(xiàn)增刪改查的全過程
實際開發(fā)中手動的輸入SQL語句是少之又少,大多數(shù)情況下是通過編譯代碼進行來控制自動執(zhí)行,下面這篇文章主要給大家介紹了關(guān)于JDBC連接SQL?Server數(shù)據(jù)庫實現(xiàn)增刪改查的相關(guān)資料,需要的朋友可以參考下2023-04-04Java httpClient連接池支持多線程高并發(fā)的實現(xiàn)
本文主要介紹了Java httpClient連接池支持多線程高并發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Java利用POI實現(xiàn)導(dǎo)入導(dǎo)出Excel表格
這篇文章主要為大家詳細介紹了Java利用POI實現(xiàn)導(dǎo)入導(dǎo)出Excel表格,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08Springboot安全框架整合SpringSecurity實現(xiàn)方式
這篇文章主要介紹了Spring全家桶中Springboot安全框架整合SpringSecurity的實現(xiàn)方式,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09IDEA 中創(chuàng)建Spring Data Jpa 項目的示例代碼
這篇文章主要介紹了IDEA 中創(chuàng)建Spring Data Jpa 項目的示例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04