Java基本數學運算之Math類


什么是Math類

  • Java操作數學運算相關的類
  • 構造函數被私有化,所以不允許創建對象
  • 都是靜態方法,使用是直接類名.方法名

常用API

//計算平⽅根
System.out.println(Math.sqrt(16));
//計算⽴⽅根
 System.out.println(Math.cbrt(8));
 //兩個數的最⼤,⽀持int, long, float,double
 System.out.println(Math.max(2.9,4.5));
 System.out.println(Math.min(2.9,4.5));
 //ceil向上取整,更⼤的值⽅向靠攏, 中⽂是天花板
 System.out.println(Math.ceil(19.7));
 System.out.println(Math.ceil(-20.1));
 //floor向下取整,更⼩的值⽅向靠攏,中⽂是地板意思
 System.out.println(Math.floor(19.7));
 System.out.println(Math.floor(-20.1));
 //隨機數
 System.out.println(Math.random()); //⼩於1⼤於0的double類型的數
 //產⽣1到10的隨機數,int⽅法進⾏轉換它會去掉⼩數掉后⾯的數字即只獲取整數部分,不是四舍
五⼊
 int random=(int)(Math.random()*10+1);

產生隨機數:math.random(),結果在0-1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM