Math類是一個靜態類,裏面提供了常用的數學計算,方便我們日常使用,是一個很常用的類。下面列舉一些常用的方法:
public class Hello {
public static void main(String[] args) {
//返回絕對值
int a=Math.abs(-1);
System.out.println(a);
//返回一個大於等於0.0且小於1.0之間的隨機double型隨機數,即 0.0<=Math.random()<1.0
double b= Math.random();
System.out.println(b);
//進一取整
double c=Math.ceil(1.5);
System.out.println(c);
//四捨五入
double d=Math.round(1.5);
System.out.println(d);
//捨去法
double e=Math.floor(1.5);
System.out.println(e);
//兩數取大
int f=Math.max(1,3);
System.out.println(f);
//兩數取小
double g=Math.min(1.53,1.51);
System.out.println(g);
}
}