Java中BigInteger的各种方法详解
生活随笔
收集整理的這篇文章主要介紹了
Java中BigInteger的各种方法详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
顧名思義,BigInteger就是用于處理題目中涉及到大整數的加減乘除運算。
文章目錄
- ·int 與 BigInteger之間的相互轉化
- ·錯誤案例:BigInteger 與 int 之間不能直接相互轉化
- ·BigInteger的常用方法
·int 與 BigInteger之間的相互轉化
import java.math.BigInteger; public class Demo1{public static void main(String[] args){//int 與 BigInteger之間的正確轉換方法://int 轉換為 BigInteger的方法:int p = 1;BigInteger a = BigInteger.valueOf(p);//BigInteger 轉換為 int的方法:BigInteger d = new BigInteger("9");int temp = d.intValue();*/} }·錯誤案例:BigInteger 與 int 之間不能直接相互轉化
import java.math.BigInteger; public class Demo2{public static void main(String[] args){BigInteger temp = new BigInteger("9");int a = 1;System.out.println(temp+a);} }·BigInteger的常用方法
import java.math.BigInteger; public class Demo3{public static void main(String[] args){//常用方法:BigInteger temp = new BigInteger("9");BigInteger temp1 = new BigInteger("6");BigInteger temp2 = new BigInteger("-6");//add();-------加法System.out.println(temp.add(temp1));//subtract();-------減法System.out.println(temp.subtract(temp1));//multiply()-------乘法System.out.println(temp.multiply(temp1));//divide()-------相除取整System.out.println(temp.divide(temp1));//remainder()與mod()-------取余System.out.println(temp.remainder(temp1));System.out.println(temp.mod(temp1));//negate()------取相反數System.out.println(temp.negate());//abs()------取絕對值System.out.println(temp2.abs());//min(),max()------取最大與最小值System.out.println(temp1.min(temp2));System.out.println(temp1.max(temp2));//gcd()------取最大公約數System.out.println(temp1.gcd(temp));} } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Java中BigInteger的各种方法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win10怎么清除世界之窗浏览器缓存 W
- 下一篇: C#——界面防卡Application.