BigInteger和BigDecimal(Java)

文章详细介绍了Java中用于处理大数据的BigInteger类和进行精确小数运算的BigDecimal类,包括它们的构造方法(如通过整数、字符串、随机数生成)和主要成员方法(如加减乘除、次幂、比较等)。特别强调了使用BigDecimal避免浮点数精度问题以及divide方法的精确除法使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BigInteger和BigDecimal(Java)

BigInteger

BigInter部分构造方法

方法名说明
public BigInteger(int num,Random rnd)获取随机大整数,范围[0~2的num次方-1]
public BigInteger(String val)获取指定的大整数
public BigInteger(String val,int redix)获取指定进制的大整数
public static BigIntegervalueOf(long val)静态方法获取BigInteger的对象,内部有优化

对象一旦创建,内部记录的值就不能发生改变

public BigInteger(int num,Random rnd)

实例演示(生成十个范围在[0,2^4-1]的数)

        for (int i = 0; i < 10; i++) {
            BigInteger bigInteger=new BigInteger(4,new Random());//生成[0~15]范围内的随机数
            System.out.println(bigInteger);
        }

结果如下

public BigInteger(String val)

实例演示(输出一个大整数)

        BigInteger bigInteger=new BigInteger("6666666666666666666666666666666666666");
        System.out.println(bigInteger);

结果如下:

在这里插入图片描述

val对应的数字只能是整数,否则会直接报错

本质上就是BigIn(String val,10);

//源码
public BigInteger(String val) {
        this(val, 10);
    }
public BigInteger(String val,int redix)

实例展示(生成一个十进制的100和一个八进制的100)

		BigInteger bigInteger1=new BigInteger("100",10);
        System.out.println(bigInteger1);
        BigInteger bigInteger2=new BigInteger("100",8);
        System.out.println(bigInteger2);

结果展示

在这里插入图片描述

输出的数是十进制,val对应的数是redix进制下的对应的数字

细节:

字符串中的数字必须是整数

字符串中的数字必须是跟进制吻合的。比如:在二进制中,只能是0和1,出现其他数字就会报错

public static BigIntegervalueOf(long val)

实例演示

        BigInteger bigInteger=BigInteger.valueOf(6666666666L);
        System.out.println(bigInteger);

演示结果

在这里插入图片描述

参数类型是Long类型的,可以输入Long类型内的任意数,超出这个范围就不行了,输入的数超过int类型的话要在末尾加上L表示long类型

long类型的范围是[-9223372036854775808,9223372036854775807]

在内部对常用的数字:-16 —16进行了优化。提前把-16—16先创建好BigInteger对象,如果多次获取不会直接创建新的

        BigInteger bigInteger1 = BigInteger.valueOf(16);
        BigInteger bigInteger2 = BigInteger.valueOf(16);
        System.out.println(bigInteger1 == bigInteger2);
        BigInteger bigInteger3 = BigInteger.valueOf(17);
        BigInteger bigInteger4 = BigInteger.valueOf(17);
        System.out.println(bigInteger3 == bigInteger4);

16时,bigInteger1和bigInteger2返回true,说明两者的地址值相同

但是17时,bigInteger3和bigInteger4返回false,说明两者的地址值不一样,两者不是一个对象

BigInter常见成员方法

方法名说明
public BigInteger add(BigInteger val)加法
public BigInteger subtract(BigInteger val)减法
public BigInteger multiply(BigInteger val)乘法
public BigInteger divide(BigInteger val)除法,获取商
public BigInteger[] divideAndRemainder(BigInteger val)除法,获取商和余数
public boolean equals(Object x)比较是否相同
public BigInteger pow(int exponent)次幂
public BigInteger max/min(BigInteger val)返回较大值/较小值
public int intValue(BigInteger val)转为int类型整数,超出范围数据有误
public BigInteger add(BigInteger val)、public BigInteger subtract(BigInteger val)、public BigInteger multiply(BigInteger val)和public BigInteger divide(BigInteger val)

实例演示

 		//创建对象
        BigInteger bigInteger1 = BigInteger.valueOf(11);
        BigInteger bigInteger2 = BigInteger.valueOf(7);
        //加法
        BigInteger add=bigInteger1.add(bigInteger2);
        System.out.println("加法:"+add);
        //减法
        BigInteger subtract=bigInteger1.subtract(bigInteger2);
        System.out.println("减法:"+subtract);
        //乘法
        BigInteger multiply=bigInteger1.multiply(bigInteger2);
        System.out.println("乘法:"+multiply);
        //除法,获取商
        BigInteger divide=bigInteger1.divide(bigInteger2);
        System.out.println("除法,求商:"+divide);

public BigInteger[] divideAndRemainder(BigInteger val)

实例演示

//创建对象
BigInteger bigInteger1 = BigInteger.valueOf(11);
BigInteger bigInteger2 = BigInteger.valueOf(7);
//除法,求商和余数
BigInteger[] divideAndRemainder = bigInteger1.divideAndRemainder(bigInteger2);
System.out.println(divideAndRemainder[0]);
System.out.println(divideAndRemainder[1]);

在这里插入图片描述

除法的求商和余数的数组只有两个长度,一个用来存商一个用来存余数

public boolean equals(Object x)

实例演示

//创建对象
BigInteger bigInteger1 = BigInteger.valueOf(11);
BigInteger bigInteger2 = BigInteger.valueOf(7);
//比较是否相同
boolean equals = bigInteger1.equals(bigInteger2);
System.out.println(equals);

public BigInteger pow(int exponent)

实例演示

//创建对象
BigInteger bigInteger1 = BigInteger.valueOf(11);
BigInteger bigInteger2 = BigInteger.valueOf(7);
//次幂
BigInteger pow = bigInteger1.pow(2);//11的二次幂
System.out.println(pow);

public BigInteger max/min(BigInteger val)

实例演示

//创建对象
        BigInteger bigInteger1 = BigInteger.valueOf(11);
        BigInteger bigInteger2 = BigInteger.valueOf(7);
//返回较大值/较小值
        BigInteger max = bigInteger1.max(bigInteger2);
        System.out.println(max);
        BigInteger min = bigInteger1.min(bigInteger2);
        System.out.println(min);

最大值和最小值是两者之中的最大值和最小值,并不是单独创建的,所以返回的那个值地址并没有发生改变

//源码
    public BigInteger max(BigInteger val) {
        return (compareTo(val) > 0 ? this : val);
    }
    public BigInteger min(BigInteger val) {
        return (compareTo(val) < 0 ? this : val);
    }
public int intValue(BigInteger val)

实例展示

//创建对象
BigInteger bigInteger1 = BigInteger.valueOf(11);
BigInteger bigInteger2 = BigInteger.valueOf(7);
//转为int类型整数,超出范围数据有误
int intValue = bigInteger1.intValue();
System.out.println(intValue);

BigDecimal

  • 用于小数的精确计算
  • 用于表示很大的小数

BigDecimal的部分构造方法

通过传递double类型的小数来创建对象
BigDecimal bigDecimal1=new BigDecimal(0.01);
BigDecimal bigDecimal2=new BigDecimal(0.09);
System.out.println(bigDecimal1);
System.out.println(bigDecimal2);

可以很明显的发现,创建的情况课输出的情况不一样的情况,这就是在存储时因为存储长度的问题导致的精度不可预知问题,所以不建议使用

通过传递字符串的方式来创建对象
BigDecimal bigDecimal1=new BigDecimal("0.01");
BigDecimal bigDecimal2=new BigDecimal("0.09");
System.out.println(bigDecimal1);
System.out.println(bigDecimal2);

此时发现不会出现double时候的不可预知问题

通过静态方法获取对象
        BigDecimal bigDecimal=BigDecimal.valueOf(0.09);
        System.out.println(bigDecimal);

也不会出现double时的不可预知问题、

细节:

  1. 如果要表示的数字不大,没有超出double的取值范围,建议使用静态方法
  2. 如果要表示耳朵数字比较大,超出了double的取值范围,建议使用String的构造方法
  3. 如果传递的是[0,10]之间的整数,那么方法会返回自己创建好的对象,不会重新new

BIgDecimal的成员方法

BigDecimal的成员方法中加减乘的跟BigInteger类似,不再讲解,只说明一下除法

方法名说明
public BigDecimal divide(BigDecimal val)除法
public BigDecimal divide(BigDecimal val,精确几位,舎入几位)除法
public BigDecimal divide(BigDecimal val)
        BigDecimal bigDecimal1 = BigDecimal.valueOf(10.0);
        BigDecimal bigDecimal2 = BigDecimal.valueOf(5.0);
        BigDecimal divide = bigDecimal1.divide(bigDecimal2);
        System.out.println(divide);

当两个数可以除尽的时候会正常显示结果,但是不能除尽的时候就会报错

        BigDecimal bigDecimal1 = BigDecimal.valueOf(10.0);
        BigDecimal bigDecimal2 = BigDecimal.valueOf(3.0);
        BigDecimal divide = bigDecimal1.divide(bigDecimal2);
        System.out.println(divide);

此时就要用到下面这种除法了

public BigDecimal divide(BigDecimal val,精确几位,舎入几位)
BigDecimal bigDecimal1 = BigDecimal.valueOf(10.0);
BigDecimal bigDecimal2 = BigDecimal.valueOf(3.0);
BigDecimal divide = bigDecimal1.divide(bigDecimal2,2, RoundingMode.HALF_UP);
System.out.println(divide);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

都不会的鲨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值