BigInteger类的方法

BigInteger类的方法
 *    divide(BigInteger val)
            返回其值为 (this / val) 的 BigInteger。
          multiply(BigInteger val)
            返回其值为 (this * val) 的 BigInteger。
          subtract(BigInteger val)
            返回其值为 (this - val) 的 BigInteger。
          add(BigInteger val)
            返回其值为 (this + val) 的 BigInteger
         remainder(BigInteger val)
            返回其值为 (this % val) 的 BigInteger。
        divideAndRemainder(BigInteger val)
            返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 返回值BigInteger[]

 1 package integer;
 2 
 3 import java.math.BigInteger;
 4 
 5 /**
 6  * BigInteger类的方法
 7  *         divide(BigInteger val) 
 8           返回其值为 (this / val) 的 BigInteger。
 9           multiply(BigInteger val) 
10           返回其值为 (this * val) 的 BigInteger。
11           subtract(BigInteger val) 
12           返回其值为 (this - val) 的 BigInteger。
13           add(BigInteger val) 
14           返回其值为 (this + val) 的 BigInteger
15          remainder(BigInteger val) 
16           返回其值为 (this % val) 的 BigInteger。
17      divideAndRemainder(BigInteger val) 
18           返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 返回值BigInteger[]
19  * @author star
20  *
21  */
22 public class BigIntegerDemo {
23     public static void main(String[] args) {
24         String s = Integer.MAX_VALUE+"";//2147483647
25         System.out.println(s);
26         
27         String s1 = Long.MAX_VALUE+"";//9223372036854775807
28         System.out.println(s1);
29         
30         BigInteger big = new BigInteger("5344444444444444778815991");
31         BigInteger big2 = new BigInteger("5344444444599");
32         //运算
33         System.out.println(big.add(big2));
34         System.out.println(big.subtract(big2));
35         System.out.println(big.multiply(big2));
36          System.out.println(big.divide(big2));
37          
38          //返回一个BigInteger[]数组
39          BigInteger b1 = new BigInteger("10");
40         BigInteger b2 = new BigInteger("2");
41         BigInteger[] res = b1.divideAndRemainder(b2);
42         System.out.println(res[0]);
43         System.out.println(res[1]);
44         
45     }
46 }
BigInteger

 

转载于:https://www.cnblogs.com/star521/p/8886305.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BigIntegerJava中提供的一个用于处理大整数的,它可以处理比long型更大的整数。BigInteger对象是不可变的,即一旦创建就不能更改其值。BigInteger提供了一系列方法来进行基本的算术操作(如加、减、乘、除和取模),以及比较、位操作、转换和格式化等功能。 下面是一个简单的示例程序,展示了如何使用BigInteger进行加、减、乘、除和取模运算: ```java import java.math.BigInteger; public class BigIntegerDemo { public static void main(String[] args) { BigInteger a = new BigInteger("12345678901234567890"); BigInteger b = new BigInteger("98765432109876543210"); // 加法 BigInteger c = a.add(b); System.out.println("a + b = " + c); // 减法 BigInteger d = a.subtract(b); System.out.println("a - b = " + d); // 乘法 BigInteger e = a.multiply(b); System.out.println("a * b = " + e); // 除法 BigInteger f = a.divide(b); System.out.println("a / b = " + f); // 取模 BigInteger g = a.mod(b); System.out.println("a % b = " + g); } } ``` 输出结果为: ``` a + b = 111111111011111111100 a - b = -86419753208641975320 a * b = 1219326311370217958019256862580270160 a / b = 0 a % b = 12345678901234567890 ``` 在上面的示例中,我们创建了两个BigInteger对象a和b,并使用add、subtract、multiply、divide和mod方法进行加、减、乘、除和取模运算。由于BigInteger对象是不可变的,因此每次运算都返回一个新的BigInteger对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值