Java中 BigInteger 的常用方法與註意事項
阿新 • • 發佈:2019-02-07
註意 imp 完美解決 big courier size vid day 有時
有時處理數字範圍較大的數字相對麻煩,但有了BigInteger就可以完美解決,BigInteger具體的範圍到底有多大,我之前查找了下,說是理論無窮大,看內存的大小(只供參考)
本文主摘:
- int 與 BigInteger之間的相互轉化方法
- 使用BigInteger時的註意事項
- BigInteger的常用方法
主摘1:
1 import java.math.*; 2 public class Day1{ 3 public static void main(String[] args){ 4 //int 與 BigInteger之間的正確轉換方法:5 //int 轉換為 BigInteger的方法: 6 int p = 1; 7 BigInteger a = BigInteger.valueOf(p); 8 9 10 //BigInteger 轉換為 int的方法: 11 BigInteger d = new BigInteger("9"); 12 int temp = d.intValue();*/ 13 } 14 }
主摘2:
註意:
1:下邊這種格式是錯誤的,錯誤的,錯誤的!別看錯,是錯誤的!BigInteger 與 int 之間是不能直接相互轉化的,別直接用
1 import java.math.*; 2 public class Day1{ 3 public static void main(String[] args){ 4 BigInteger temp = new BigInteger("9"); 5 int a = 1; 6 System.out.println(temp+a); 7 } 8 }
2:使用BigInteger時要引用:import java.math.*;
主摘3:
1 import java.math.*; 2 public class Day1{ 3 public static void main(String[] args){ 4 //常用方法: 5 BigInteger temp = new BigInteger("9"); 6 BigInteger temp1 = new BigInteger("6"); 7 BigInteger temp2 = new BigInteger("-6"); 8 //add();-------加法 9 System.out.println(temp.add(temp1)); 10 //subtract();-------減法 11 System.out.println(temp.subtract(temp1)); 12 //multiply()-------懲罰 13 System.out.println(temp.multiply(temp1)); 14 //divide()-------相除取整 15 System.out.println(temp.divide(temp1)); 16 //remainder()與mod()-------取余 17 System.out.println(temp.remainder(temp1)); 18 System.out.println(temp.mod(temp1)); 19 //negate()------取相反數 20 System.out.println(temp.negate()); 21 //abs()------取絕對值 22 System.out.println(temp2.abs()); 23 //min(),max()------取最大與最小值 24 System.out.println(temp1.min(temp2)); 25 System.out.println(temp1.max(temp2)); 26 //gcd()------取最大公約數 27 System.out.println(temp1.gcd(temp)); 28 } 29 }
如有個別回答錯誤,評論指出,我必更改,謝謝!??
Java中 BigInteger 的常用方法與註意事項