public class Main {
public static void main(String[] args) {
/*
public static int abs(int a) 取绝对值
public static double ceil(double a) 向上取整
public static double floor(double a) 向下取整
public static double round(double a) 四舍五入
public static int max(int a,int b) 获取两个整数的最大值
public static int min(int a,int b) 获取两个整数的最小值
public static double pow(double a,double b) 返回a的b次幂值
public static double sqrt(double a) 返回a的平方根
public static double cbrt(double a) 返回a的立方根
public static double random() 返回值为double的随机值,范围为[0.0,1.0);
*/
System.out.println(Math.abs(-23));//取绝对值
// Bug:int的取值范围是-2147483648 ~ 2147483647
// 如果正数没有与之相对应的负数,那么他就会取错
// 例如: System.out.println(Math.abs(-2147483648)); 结果为-2147483648
System.out.println(Math.ceil(12.34));//13.0
System.out.println(Math.ceil(-13.34));//-13.0
System.out.println(Math.floor(12.34));//12.0
System.out.println(Math.floor(-13.34));//-14.0
System.out.println(Math.round(12.35));//12
System.out.println(Math.round(12.67));//13
System.out.println(Math.max(23,34));//34
System.out.println(Math.max(-11,-23));//-11
System.out.println(Math.min(21,23));//21
System.out.println("2的3次幂:"+Math.pow(2, 3));//8.0
System.out.println("16的平方根:"+Math.sqrt(16));//4
System.out.println("8的立方根:"+Math.cbrt(8));//2
for (int i = 0; i < 10; i++) {
System.out.println(Math.random());
}
}
}
Java常用API——Math
最新推荐文章于 2024-08-31 10:44:02 发布