java數(shù)學(xué)工具類Math詳解(round方法)
數(shù)學(xué)工具類Math,供大家參考,具體內(nèi)容如下
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)定義好的圓周率,非常精確
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合FastDFS中間件實現(xiàn)文件分布管理
FastDFS是一個開源的輕量級分布式文件系統(tǒng),它對文件進行管理,功能包括:文件存儲、文件同步、文件上傳、文件下載等,解決了大容量存儲和負載均衡的問題,本文介紹了SpringBoot整合FastDFS中間件實現(xiàn)文件分布管理,需要的朋友可以參考下2024-08-08
java實現(xiàn)騰訊ocr圖片識別接口調(diào)用
這篇文章主要為大家詳細介紹了java實現(xiàn)騰訊ocr圖片識別接口調(diào)用,拍車牌識別車牌號功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
SpringCloud之Zuul網(wǎng)關(guān)原理及其配置講解
這篇文章主要介紹了SpringCloud之Zuul網(wǎng)關(guān)原理及其配置講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot中如何對actuator進行關(guān)閉
這篇文章主要介紹了SpringBoot中如何對actuator進行關(guān)閉問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Java使用pdfbox實現(xiàn)給pdf文件加圖片水印
有時候需要給pdf加水印,市面上工具都是收費的要會員,還是自食其力吧;嘗試過 spire.pdf.free 那個超過10頁就不行了!所以本文還是使用了pdfbox,感興趣的可以了解一下2022-11-11
SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能
這篇文章主要介紹了SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能,需要的朋友可以參考下2018-06-06
詳解jenkins自動部署springboot應(yīng)用的方法
這篇文章主要介紹了詳解jenkins自動部署springboot應(yīng)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

