第二章.数字相关类

1.Math类介绍

1、概述:Math是一个数学工具类

2.作用:主要用于数学运算

3.特点:

  • a.构造私有化,不能new对象
  • b.方法都是静态的

4.使用: 类名直接调用

2.Math类方法

static int abs(int a) -> 求参数的绝对值
static double ceil(double a) -> 向上取整
static double floor(double a) ->向下取整
static long round(double a) -> 四舍五入
static int max(int a, int b) ->求两个数之间的较大值
static int min(int a, int b) ->求两个数之间的较小值

public class Demo01Math {
    public static void main(String[] args) {
        //static int abs(int a) -> 求参数的绝对值
        System.out.println("Math.abs(1) = " + Math.abs(1));
        System.out.println("Math.abs(-1) = " + Math.abs(-1));
        //static double ceil(double a) -> 向上取整
        System.out.println("Math.ceil(2.6) = " + Math.ceil(2.6));
        System.out.println("Math.ceil(-2.6) = " + Math.ceil(-2.6));
        //static double floor(double a) ->向下取整
        System.out.println("Math.floor(2.4) = " + Math.floor(2.4));
        //static long round(double a) -> 四舍五入
        System.out.println("Math.round(2.6) = " + Math.round(2.6));
        System.out.println("Math.round(-1.8) = " + Math.round(-1.8));
        //static int max(int a, int b) ->求两个数之间的较大值
        System.out.println("Math.max(10,20) = " + Math.max(10, 20));
        //static int min(int a, int b) ->求两个数之间的较小值
        System.out.println("Math.min(10,20) = " + Math.min(10, 20));
    }
}

round()方法的实现原理:先给参数+0.5,取小于或者等于这个数的最大整数

第三章.BigInteger

1.BigInteger介绍

  • 1.概述:将来我们操作的整数,有可能非常大,大到比long型还要大,这种整数应该称之为"对象",我们需要用 BigInteger来接收超大整数
  • 2.作用: 处理超大整数

2.BigInteger使用

1.构造:
BigInteger(String val) -> val要求是数字形式 "12121212"
2.方法:
BigInteger add(BigInteger val) 返回其值为 (this + val) 的 BigInteger。->加法
BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger。->减
法
BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger->乘法
BigInteger divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger->除法

public class Demo01BigInteger {
    public static void main(String[] args) {
        BigInteger b1 = new BigInteger("121212121212121212121212121212121212");
        BigInteger b2 = new BigInteger("121212121212121212121212121212121212");
        BigInteger add = b1.add(b2);
        System.out.println("add = " + add);
        BigInteger subtract = b1.subtract(b2);
        System.out.println("subtract = " + subtract);
        BigInteger multiply = b1.multiply(b2);
        System.out.println("multiply = " + multiply);
        BigInteger divide = b1.divide(b2);
        System.out.println("divide = " + divide);
    }
}

第四章.BigDecimal类

1.BigDecimal介绍

注意:

1、我们不能直接用double和float类型的数据直接参与运算,因为有可能会出现精度损失问题

2.解决:我们可以用BigDecimal来解决

3.作用:解决double和float的精度损失问题

2.BigDecimal使用

1.构造:
BigDecimal(String val)
2.方法:
BigDecimal add(BigDecimal val) 返回其值为 (this + val) 的 BigDecimal。->加法
BigDecimal subtract(BigDecimal val) 返回其值为 (this - val) 的 BigDecimal。->减
法
BigDecimal multiply(BigDecimal val) 返回其值为 (this * val) 的 BigDecimal->乘法
BigDecimal divide(BigDecimal val) 返回其值为 (this / val) 的 BigDecimal->除法
3.注意:如果除不尽,会报错

public class Demo02BigDecimal {
    public static void main(String[] args) {
        BigDecimal b1 = new BigDecimal("3.15");
        BigDecimal b2 = new BigDecimal("2.12");
        BigDecimal add = b1.add(b2);
        System.out.println("add = " + add);
        BigDecimal subtract = b1.subtract(b2);
        System.out.println("subtract = " + subtract);
        BigDecimal multiply = b1.multiply(b2);
        System.out.println("multiply = " + multiply);
        //BigDecimal divide = b1.divide(b2);
        //System.out.println("divide = " + divide); 如果除不尽,报错
    }
}

BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
divisor:代表的是要除的那个数
scale:保留几位小数
roundingMode:取舍方式
static int ROUND_UP :向上加1
static int ROUND_DOWN :直接舍去
static int ROUND_HALF_UP:四舍五入

public class Demo03BigDecimal {
    public static void main(String[] args) {
        BigDecimal b1 = new BigDecimal("3.15");
        BigDecimal b2 = new BigDecimal("2.12");
        BigDecimal divide = b1.divide(b2,2,BigDecimal.ROUND_UP);
        System.out.println("divide = " + divide);
    }
}

注意:以后开发不要用double和float直接做运算

总结

简单记录,后续会继续输出。

相关推荐

  1. 第二.数字相关

    2024-06-12 23:44:07       32 阅读
  2. 计算机网络相关题目及答案(第二

    2024-06-12 23:44:07       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 23:44:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 23:44:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 23:44:07       87 阅读
  4. Python语言-面向对象

    2024-06-12 23:44:07       96 阅读

热门阅读

  1. Linux内核 -- ftrace 调试工具培训

    2024-06-12 23:44:07       33 阅读
  2. 第一章 - 第1节-计算机概述 - 课后习题

    2024-06-12 23:44:07       30 阅读
  3. CSS Display(显示)

    2024-06-12 23:44:07       28 阅读
  4. 新建pdb 打不开 ORA-65104 ORA-25153

    2024-06-12 23:44:07       28 阅读
  5. 哲学家进餐问题

    2024-06-12 23:44:07       32 阅读
  6. ARM 汇编 C语言 for循环

    2024-06-12 23:44:07       26 阅读
  7. day7C++

    2024-06-12 23:44:07       22 阅读
  8. 解封装类的实现【3】

    2024-06-12 23:44:07       28 阅读
  9. <题海拾贝>[递归]2.合并两个有序链表

    2024-06-12 23:44:07       31 阅读
  10. Element ui 快速入门

    2024-06-12 23:44:07       31 阅读