biginteger的用法
BigInteger類型的數(shù)字范圍較Integer,Long類型的數(shù)字范圍要大得多,它支持任意精度的整數(shù),也就是說在運(yùn)算中 BigInteger 類型可以準(zhǔn)確地表示任何大小的整數(shù)值而不會丟失任何信息。
//讀入方法:nextBigInteger()
public void test5() {
Scanner scan = new Scanner(System.in); // 讀入
int n = scan.nextInt(); // 讀入一個(gè)int;
BigInteger m = scan.nextBigInteger(); // 讀入一個(gè)BigInteger;
//基本運(yùn)算:add(),subtract(),multiply(),divide(),mod(),remainder(),pow(),abs(),negate()
public void testBasic() {
BigInteger a = new BigInteger("13");
BigInteger b = new BigInteger("4");
int n = 3;
//1.加
BigInteger bigNum1 = a.add(b); //17
//2.減
BigInteger bigNum2 = a.subtract(b); //9
//3.乘
BigInteger bigNum3 = a.multiply(b); //52
//4.除
BigInteger bigNum4 = a.divide(b); //3
//5.取模(需 b > 0,否則出現(xiàn)異常:ArithmeticException("BigInteger: modulus not positive"))
BigInteger bigNum5 = a.mod(b); //1
//6.求余
BigInteger bigNum6 = a.remainder(b); //1
//7.平方(需 n >= 0拌夏,否則出現(xiàn)異常:ArithmeticException("Negative exponent"))
BigInteger bigNum7 = a.pow(n); //2197
//8.取絕對值
BigInteger bigNum8 = a.abs(); //13
//9.取相反數(shù)
BigInteger bigNum9 = a.negate(); //-13
}
任意類型互相轉(zhuǎn)換
.valueOf()可以應(yīng)用到任何數(shù)據(jù)類型,且不會有異常報(bào)錯(cuò)履因。
例如吧int轉(zhuǎn)換成biginteger
int a=1;
biginteger? c=biginteger .valueOf(a);
2.StringBuilder類轉(zhuǎn)換為String類
String s = sb.toString();