BigInteger
java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符.
BigInteger类的构造方法:
BigInteger b = new BigInteger(str);
构造方法中,采用字符串的形式给出整数
四则运算代码:
public static void main(String[] args) {
//大数据封装为BigInteger对象
BigInteger big1 = new BigInteger("12345678909876543210");
BigInteger big2 = new BigInteger("98765432101234567890");
//add实现加法运算
BigInteger bigAdd = big1.add(big2);
//subtract实现减法运算
BigInteger bigSub = big1.subtract(big2);
//multiply实现乘法运算
BigInteger bigMul = big1.multiply(big2);
//divide实现除法运算
//保留scale位小数,之后的数据四舍五入
BigInteger bigDiv = big2.divide(big1,scale,BigDecimal.ROUND_HALF_UP);
}
biginteger怎么比较大小
compareTo方法来比较,小于则返回-1,等于则返回0,大于则返回1
BigInteger a1 = new BigInteger("10");
BigInteger a2 = new BigInteger("20");
BigInteger a3 = new BigInteger("20");
int b1 = a1.compareTo(a2);//b1为-1
int b2 = a2.compareTo(a1);//b2为1
int b3 = a2.compareTo(a3);//b3为0