Java中的中的BigInteger类与类与BigDecimal类,细解,带例题类,细解,带例题
BigInteger
(大整数类)
使用时导包导包:java.math.BigInteger
作用作用:可以使用BigInteger操作大整数,也可以转换进制。如果在操作的时候一个整型数据已经超过了整数的最大类型长度
long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作。这些大数都会以字符串的形式传入。
一、常用构造方法一、常用构造方法:
1.
BigInteger(int numBit,Random rnd)
构造一个随机生成的 BigInteger,它是在 0 到 (2^numBits – 1)(包括)范围内均匀分布的值。
eg:
生成五个范围在0 到 (2^1000 – 1)的随机整数
import java.math.BigInteger;
import java.util.Random;
public class 大整数 {
public static void main(String[] args) {
Random rnd=new Random();
for (int i = 0; i < 5; i++) {
BigInteger a=new BigInteger(1000, rnd);
System.out.println(a);
}
}
}
BigInteger(String val)
将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
eg:
public class 大整数_1 {
public static void main(String[] args) {
BigInteger a= new BigInteger("1323465413");
System.out.println(a);//1323465413
}
}
BigInteger(String val, int radix)
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。
eg:
public class 大整数_2 {
public static void main(String[] args) {
BigInteger a= new BigInteger("11",2);
System.out.println(a);//3
}
}
二、二、BigInteger常量常量:
BigInteger.ONE ——>1
BigInteger.TEN ——>10
BigInteger.ZERO—–>0
eg:
public class 大整数_3 {
public static void main(String[] args) {
BigInteger a=BigInteger.ONE;
BigInteger b= BigInteger.TEN;
System.out.println(a);//1