Java常用API之BigInteger与BigDecimal的常用API总结

BigInteger类

不可以改变的任意精度的整数,底层使用的字符串进行实现的,int类型的“+、-、*、/”等在这里都不能使用,但是里面包装了相关的运算的方法。

  • BigInteger(String val):通过字符串生成一个BigInteger对象
  • BigInteger add(BigInteger val):将两个BigInteger对象进行相加运算,返回值也是一个BigInteger对象
  • BigInteger subtract(BigInteger val):将两个BigInteger对象进行相减运算,返回值也是一个BigInteger对象
  • BigInteger multiply(BigInteger val):将两个BigInteger对象进行乘法运算,返回值也是一个BigInteger对象
  • BigInteger divide(BigInteger val):将两个BigInteger对象进行除法运算,返回值也是一个BigInteger对象
  • BigInteger remainder(BigInteger val):取余,将两个BIgInteger对象进行取余运算,返回值也是一个BigInteger对象
  • int intValue():将BigInteger对象转换为一个int类型的数
  • long longValue():将BigInteger对象转换为一个long类型的数
  • float floatValue():将BigInteger对象转换为一个float类型的数

代码实例:

public static void main(String[] args) {
        long longNum = 457654778540878935L;
        BigInteger bigInteger1 = new BigInteger(String.valueOf(longNum));
        System.out.println("bigInteger1 = " + bigInteger1);
        BigInteger bigInteger2 = new BigInteger(String.valueOf(longNum));
        System.out.println("bigInteger2 = " + bigInteger2);
        
        BigInteger add = bigInteger1.add(bigInteger2);
        System.out.println("add = " + add);
        BigInteger subtract = bigInteger1.subtract(bigInteger2);
        System.out.println("subtract = " + subtract);
        BigInteger multiply = bigInteger1.multiply(bigInteger2);
        System.out.println("multiply = " + multiply);
        BigInteger divide = bigInteger1.divide(bigInteger2);
        System.out.println("divide = " + divide);
        BigInteger remainder = bigInteger1.remainder(bigInteger2);
        System.out.println("remainder = " + remainder);
        
        int intValue = bigInteger1.intValue();
        System.out.println("intValue = " + intValue);
        double doubleValue = bigInteger1.doubleValue();
        System.out.println("doubleValue = " + doubleValue);
        float floatValue = bigInteger1.floatValue();
        System.out.println("floatValue = " + floatValue);
        long longValue = bigInteger1.longValue();
        System.out.println("longValue = " + longValue);
    }

结果:
BigInteger测试

BigDecimal类

不可变的任意精度的有符号十进制数。底层也是使用的字符串进行实现的。
在我们平常使用double的时候经常会碰见精度问题,比如我们传入的12323.20 或许会变成12323.199999999,这些是double类型的精度问题,我们不可避免,但是BigDecimal就能够很好的避免这样的问题。和BigInteger代表任意大小的整型一样,BigDecimal代表了任意精度、任意大小的十进制数,只要你的内存够大,它就能足够大。

常用API:

  • BigDecimal(String val):将BigDecimal的字符串表示 BigDecimal转换为 BigDecimal 。
  • BigDecimal(BigInteger val) :将 BigInteger转换成 BigDecimal 。
  • BigDecimal add(BigDecimal val) :传入一个BigDecimal对象,将两者进行相加,返回给BigDecimal类型引用
  • BigDecimal subtract(BigDecimal val):传入一个BigDecimal类型,两者进行相减,返回一个BigDecimal类型对象。
  • BigDecimal multiply(BigDecimal val) :将两个BigDecimal对象进行相乘,返回一个BigDecimal对象
  • BigDecimal divide(BigDecimal val) :将两个BigDecimal对象进行相除,返回一个BigDecimal对象
  • BigDecimal divide(BigDecimal divisor, int roundingMode) :将两个BigDecimal对象进行相除,返回一个BigDecimal对象,roundingMode表示指定的舍入模式
  • BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) :将两个BigDecimal对象进行相除,返回一个BigDecimal对象,scale表示取几位小数,roundingMode表示指定的舍入模式
  • BigDecimal remainder(BigDecimal val): 将两个BigDecimal对象进行取余,返回一个BigDecimal对象
  • double doubleValue():将此 BigDecimal 转换为 double。

代码实例:

public static void main(String[] args) {
        long longNum = 457654778540878935L;
        BigDecimal bigDecimal = new BigDecimal(longNum);
        BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(3));
        System.out.println("bigDecimal = " + bigDecimal);
        System.out.println("bigDecimal = " + bigDecimal);
        BigDecimal add = bigDecimal.add(bigDecimal);
        System.out.println("add = " + add);
        BigDecimal subtract = bigDecimal.subtract(bigDecimal);
        System.out.println("subtract = " + subtract);
        BigDecimal multiply = bigDecimal.multiply(bigDecimal);
        System.out.println("multiply = " + multiply);
        BigDecimal divide = bigDecimal.divide(bigDecimal2);
        System.out.println("divide = " + divide);
        BigDecimal divideRoundMode = bigDecimal.divide(bigDecimal2, BigDecimal.ROUND_UP); //进位
        System.out.println("divideRoundMode = " + divideRoundMode);
        BigDecimal divideScale = bigDecimal.divide(bigDecimal2, 3, BigDecimal.ROUND_DOWN); //去除多位
        System.out.println("divideRoundScale = " + divideScale);
        BigDecimal remainder = bigDecimal.remainder(bigDecimal);
        System.out.println("remainder = " + remainder);
        int intValue = bigDecimal.intValue();
        System.out.println("intValue = " + intValue);
        double doubleValue = bigDecimal.doubleValue();
        System.out.println("doubleValue = " + doubleValue);
        float floatValue = bigDecimal.floatValue();
        System.out.println("floatValue = " + floatValue);
        long longValue = bigDecimal.longValue();
        System.out.println("longValue = " + longValue);
    }

结果:
BigDecimal测试

除此之外: BigInteger和BigDecimal类中还包括了

  • abs()
  • compareTo(BigDecimal val)
  • pow(int n)
  • max(BigDecimal val)
  • min(BigDecimal val)
  • pow(int n)
  • 由于方法过多,这里就不列举了。

注意: 在包装类中除了Float类和Double类没有缓冲区,在这里BigInteger也有一个类似的缓冲区,Integer的缓冲区大小是 -128到127,而BigInteger的缓冲区大小为 -16到16

缓冲区:在缓冲区数值范围内的会直接在内存中取出对象,超过范围的会创建新的对象。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑妖问路

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

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

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

打赏作者

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

抵扣说明:

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

余额充值