BigInterger的加减乘除例子
时间: 2023-10-28 11:38:50 浏览: 72
当然可以,以下是一些基本的BigInteger加减乘除的例子:
加法:
BigInteger a = new BigInteger("123456789");
BigInteger b = new BigInteger("987654321");
BigInteger c = a.add(b);
System.out.println(c); // 输出 1111111110
减法:
BigInteger a = new BigInteger("987654321");
BigInteger b = new BigInteger("123456789");
BigInteger c = a.subtract(b);
System.out.println(c); // 输出 864197532
乘法:
BigInteger a = new BigInteger("123456789");
BigInteger b = new BigInteger("987654321");
BigInteger c = a.multiply(b);
System.out.println(c); // 输出 121932631137655779
除法:
BigInteger a = new BigInteger("987654321");
BigInteger b = new BigInteger("123456789");
BigInteger c = a.divide(b);
System.out.println(c); // 输出 8
希望这些例子对你有帮助!
相关问题
字符串转BigInterger
要将字符串转换为BigInteger,有两种方法可以实现。
方法1:使用BigInteger类的构造方法BigInteger(String val)。这个构造方法接受一个表示数字的字符串作为参数,并将其转换为BigInteger对象。例如,如果要将字符串"123456789"转换为BigInteger对象,可以使用以下代码:
```
String str = "123456789";
BigInteger bigInteger = new BigInteger(str);
```
<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [【Java】int、String、BigInteger的类型转换](https://blog.csdn.net/weixin_43896318/article/details/130391242)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文