一、基本介绍
1、BigInteger 适合保存比较大的整型
2、BigDecimal 适合保存精度更高的浮点型(小数)
二、BigInteger常见方法
1、add:加
2、subtract:减
3、multiply:乘
4、divide:除
public class BigInteger_ {
public static void main(String[] args) {
//当我们编程中,需要处理很大的整数
//可以使用BigInteger
BigInteger bigInteger = new BigInteger("99999999999999999999");
BigInteger bigInteger1 = new BigInteger("1000");
System.out.println(bigInteger);
//在对 BigInteger 加减乘除的时候,需要对应的方法 不能直接 + - * /
//创建一个需要操作的 BigInteger 进行加减乘除
BigInteger n1 = bigInteger.add(bigInteger1);//加
System.out.println(n1);
BigInteger n2 = bigInteger.subtract(bigInteger1);//减
System.out.println(n2);
BigInteger n3 = bigInteger.multiply(bigInteger1);//乘
System.out.println(n3);
BigInteger n4 = bigInteger.divide(bigInteger1);//除
System.out.println(n4);
}
}
三、BigDecimal常见方法
1、add:加
2、subtract:减
3、multiply:乘
4、divide:除
public class BigDecimal_ {
public static void main(String[] args) {
//当我们编程中,需要处理很大的整数
//可以使用BigInteger
BigDecimal bigDecimal = new BigDecimal("9.9999999999999999999999");
BigDecimal bigDecimal1 = new BigDecimal("1.12");
System.out.println(bigDecimal);
//在对 BigDecimal 加减乘除的时候,需要对应的方法 不能直接 + - * /
//创建一个需要操作的 BigInteger 进行加减乘除
BigDecimal n1 = bigDecimal.add(bigDecimal1);//加
System.out.println(n1);
BigDecimal n2 = bigDecimal.subtract(bigDecimal1);//减
System.out.println(n2);
BigDecimal n3 = bigDecimal.multiply(bigDecimal1);//乘
System.out.println(n3);
//有可能抛出异常
//在调用divide方法时,指定精度即可 BigDecimal.ROUND_CEILING
//如果无限循环 就会保留分子的精度
BigDecimal n4 = bigDecimal.divide(bigDecimal1, BigDecimal.ROUND_CEILING);//除
System.out.println(n4);
}
}