BigInteger的加减乘除
时间: 2024-05-08 11:17:09 浏览: 65
BigInteger是Java中的一个类,用于处理大整数运算。BigInteger支持加、减、乘、除等运算,以下是示例代码:
加法:
```
BigInteger a = new BigInteger("123456789");
BigInteger b = new BigInteger("987654321");
BigInteger c = a.add(b);
System.out.println(c.toString()); // 输出:1111111110
```
减法:
```
BigInteger a = new BigInteger("987654321");
BigInteger b = new BigInteger("123456789");
BigInteger c = a.subtract(b);
System.out.println(c.toString()); // 输出:864197532
```
乘法:
```
BigInteger a = new BigInteger("123456789");
BigInteger b = new BigInteger("987654321");
BigInteger c = a.multiply(b);
System.out.println(c.toString()); // 输出:121932631137021795
```
除法:
```
BigInteger a = new BigInteger("987654321");
BigInteger b = new BigInteger("123456789");
BigInteger c = a.divide(b);
System.out.println(c.toString()); // 输出:8
```
以上是简单的示例,实际应用中需要考虑数据类型的范围、溢出问题等。
阅读全文