BigInteger的常用方法

概述:

        java.math.BigInteger 类表示一个超大的整数,可以存储很长的数

构造方法:

    //将 BigInteger 的十进制字符串表示形式转换为 BigInteger
    public BigInteger(String val);

成员方法:

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。除不尽的取整数

成员方法演示:        

        // 创建一个BigInteger对象
        BigInteger bigInteger1 = new BigInteger("123456789987654321123456789");
        BigInteger bigInteger2 = new BigInteger("123456789987612345678998769");

        // b1 + b2
        BigInteger result1 = bigInteger1.add(bigInteger2);
        System.out.println(result1);

        // b1 - b2
        BigInteger result2 = bigInteger1.subtract(bigInteger2);
        System.out.println(result2);

        // b1 * b2
        BigInteger result3 = bigInteger1.multiply(bigInteger2);
        System.out.println(result3);

        // b1 / b2    除不尽的取整数
        BigInteger result4 = bigInteger1.divide(bigInteger2);
        System.out.println(result4);

        输出结果:
                246913579975266666802455558
                41975444458020
                15241578994050602077583661310619524130818061755692741
                1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值