Java中Math類常用方法代碼詳解
近期用到四舍五入想到以前整理了一點(diǎn),就順便重新整理好經(jīng)常見到的一些四舍五入,后續(xù)遇到常用也會(huì)直接在這篇文章更新。。。
public class Demo{
public static void main(String args[]){
/**
*Math.sqrt()//計(jì)算平方根
*Math.cbrt()//計(jì)算立方根
*Math.pow(a, b)//計(jì)算a的b次方
*Math.max( , );//計(jì)算最大值
*Math.min( , );//計(jì)算最小值
*/
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求絕對(duì)值
*/
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 取得一個(gè)大于或者等于0.0小于不等于1.0的隨機(jī)數(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í)候會(huì)取偶數(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時(shí)返回int值,double時(shí)返回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類常用方法代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留
言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JDBC連接SQL?Server數(shù)據(jù)庫實(shí)現(xiàn)增刪改查的全過程
實(shí)際開發(fā)中手動(dòng)的輸入SQL語句是少之又少,大多數(shù)情況下是通過編譯代碼進(jìn)行來控制自動(dòng)執(zhí)行,下面這篇文章主要給大家介紹了關(guān)于JDBC連接SQL?Server數(shù)據(jù)庫實(shí)現(xiàn)增刪改查的相關(guān)資料,需要的朋友可以參考下2023-04-04
Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn)
本文主要介紹了Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
使用Java程序模擬實(shí)現(xiàn)新冠病毒傳染效果
這篇文章主要介紹了用Java程序模擬實(shí)現(xiàn)新冠病毒傳染效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格
這篇文章主要為大家詳細(xì)介紹了Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Springboot安全框架整合SpringSecurity實(shí)現(xiàn)方式
這篇文章主要介紹了Spring全家桶中Springboot安全框架整合SpringSecurity的實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09
IDEA 中創(chuàng)建Spring Data Jpa 項(xiàng)目的示例代碼
這篇文章主要介紹了IDEA 中創(chuàng)建Spring Data Jpa 項(xiàng)目的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

