Math类概述

  • Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

    成员方法

  • public static int abs(int a)
  • public static double ceil(double a)
  • public static double floor(double a)
  • public static int max(int a,int b)
  • public static double pow(double a,double b)
  • public static double random()
  • public static int round(float a)
  • public static double sqrt(double a)
  1. public static void main(String[] args) {
  2. System.out.println(Math.PI);//3.141592653589793
  3. System.out.println(Math.abs(-10)); //10 //取绝对值
  4. //ceil天花板
  5. /*
  6. * 13.0
  7. * 12.3
  8. * 12.0
  9. */
  10. System.out.println(Math.ceil(12.3));//13.0 //向上取整,但是结果是一个double
  11. System.out.println(Math.ceil(12.99));//13.0
  12. System.out.println("-----------");
  13. //floor地板
  14. /*
  15. * 13.0
  16. * 12.3
  17. * 12.0
  18. */
  19. System.out.println(Math.floor(12.3));//12.0 //向下取整,但是结果是一个double
  20. System.out.println(Math.floor(12.99));//12.0
  21. //获取两个值中的最大值
  22. System.out.println(Math.max(20, 30));//30
  23. //前面的数是底数,后面的数是指数
  24. System.out.println(Math.pow(2, 3));//8.0 //2.0 ^ 3.0
  25. //生成0.0到1.0之间的所以小数,包括0.0,不包括1.0
  26. System.out.println(Math.random());//随机值:0.8330543194519255
  27. //四舍五入
  28. System.out.println(Math.round(12.3f));//12
  29. System.out.println(Math.round(12.9f));//13
  30. //开平方
  31. System.out.println(Math.sqrt(4));//2.0
  32. System.out.println(Math.sqrt(2));//1.414
  33. System.out.println(Math.sqrt(3));//1.732
  34. }