数学工具类Math

  1. package com.Demo06;
  2. /*
  3. java.util.Math类时数相关的工具类,里面提供了大量的静态方法,完成数学相关的操作
  4. public static double abs(double num):获取绝对值,有多种重载
  5. public static double ceil(double num):向上取整
  6. public static double floor(double num):向下取整
  7. public static long round(double num):四舍五入
  8. Math.PI 代表近似的圆周率常量 (double)
  9. */
  10. public class Demo01Math {
  11. public static void main(String[] args) {
  12. //绝对值
  13. System.out.println(Math.abs(0.1)); //0.1
  14. System.out.println(Math.abs(-0.1)); //0.1
  15. System.out.println(Math.abs(1)); //1
  16. //向上取整
  17. System.out.println(Math.ceil(2.1)); //3.0
  18. System.out.println(Math.ceil(2.9)); //3.0
  19. System.out.println(Math.ceil(2.0)); //2.0
  20. //向下取整
  21. System.out.println(Math.floor(3.1)); //3.0
  22. System.out.println(Math.floor(3.9)); //3.0
  23. System.out.println(Math.floor(3.0)); //3.0
  24. //四舍五入
  25. System.out.println(Math.round(4.4)); //4
  26. System.out.println(Math.round(4.9)); //5
  27. System.out.println(Math.round(4.5)); //5
  28. }
  29. }