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)文章
Java并發(fā)編程之CountDownLatch原理詳解
這篇文章主要介紹了Java并發(fā)編程之CountDownLatch原理詳解,CountDownLatch類中使用了一個繼承自AQS的共享鎖Sync對象,構(gòu)造CountDownLatch對象時會將傳入的線程數(shù)值設(shè)為AQS的state值,需要的朋友可以參考下2023-12-12
Java 實戰(zhàn)范例之進銷存管理系統(tǒng)的實現(xiàn)
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+vue+Springboot+ssm+mysql+maven+redis實現(xiàn)一個前后端分離的進銷存管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
深入探討Druid動態(tài)數(shù)據(jù)源的實現(xiàn)方式
Druid是一個高性能的實時分析數(shù)據(jù)庫,它可以處理大規(guī)模數(shù)據(jù)集的快速查詢和聚合操作,在Druid中,動態(tài)數(shù)據(jù)源是一種可以在運行時動態(tài)添加和刪除的數(shù)據(jù)源,使用動態(tài)數(shù)據(jù)源,您可以在Druid中輕松地處理不斷變化的數(shù)據(jù)集,本文講給大家介紹一下Druid動態(tài)數(shù)據(jù)源該如何實現(xiàn)2023-08-08
MyBatis Plus插件機制與執(zhí)行流程原理分析詳解
這篇文章主要介紹了MyBatis Plus插件機制與執(zhí)行流程原理分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
詳解Java設(shè)計模式編程中的Flyweight享元模式的開發(fā)結(jié)構(gòu)
這篇文章主要介紹了Java設(shè)計模式編程中的Flyweight享元模式的開發(fā)結(jié)構(gòu),享元模式能夠最大限度地重用現(xiàn)有的同類對象,需要的朋友可以參考下2016-04-04
關(guān)于動態(tài)參數(shù)使用@PathVariable的解析
這篇文章主要介紹了關(guān)于動態(tài)參數(shù)使用@PathVariable的解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

