Java 数学工具类Math

朱雀 2021-02-25 17:44 671阅读 0赞

数学工具类Math

  • java.util.Math 类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作
  • public static double abs(double num):获取绝对值
  • public static double ceil(double num):向上取整
  • public static double floor(double num):向下取整
  • public static long round(double num):四舍五入

代码:

  1. public class a32_数学工具类Math {
  2. public static void main(String[] args) {
  3. // 绝对值
  4. System.out.println(Math.abs(-2));
  5. System.out.println(Math.abs(2));
  6. System.out.println(Math.abs(0));
  7. System.out.println("---------");
  8. // 向上取整
  9. System.out.println(Math.ceil(8.2));
  10. System.out.println(Math.ceil(8.98));
  11. System.out.println(Math.ceil(2.3));
  12. System.out.println("---------");
  13. // 向下取整
  14. System.out.println(Math.floor(9.4));
  15. System.out.println(Math.floor(9.42));
  16. System.out.println(Math.floor(4.65));
  17. System.out.println("---------");
  18. // 四舍五入
  19. System.out.println(Math.round(5.6));
  20. System.out.println(Math.round(5.3));
  21. System.out.println(Math.round(4.9));
  22. System.out.println("---------");
  23. // 圆周率
  24. System.out.println(Math.PI);
  25. }
  26. }

在这里插入图片描述

练习

找出-10.8到5.9中绝对值大于6或小于2.1的整数

  1. public class a33_小学数学真题 {
  2. public static void main(String[] args) {
  3. double num01 = Math.ceil(-10.8);
  4. int num02 = (int)num01;
  5. int a = 0;
  6. for (int i = num02; i <= 5.9; i++){
  7. if (Math.abs(i) > 6 || Math.abs(i) < 2.1) {
  8. a++;
  9. }
  10. }
  11. System.out.println(a);
  12. }
  13. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,671人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java 数学工具Math

    数学工具类Math java.util.Math 类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作 public static double ab...