一、应用场景
1. BigInteger 类
目前,我们学过最大的是long类型,但是,在实际开发时候,很有可能遇见超出long类型范围的数,我们就需要用BigInteger类;
① add 加
② subtract 减
③ multiply 乘
④ divide 除
public class BigInteger_ {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("299999999999999999999999999999999");
System.out.println(bigInteger);
// 在对BigInteger类型的数据进行 加减乘除 时,不能直接+ - * /
// 要使用相应的方法
BigInteger bigInteger1 = new BigInteger("100");
// 1.加
BigInteger add = bigInteger.add(bigInteger1);
System.out.println(add);
// 2.减
BigInteger subtract = bigInteger.subtract(bigInteger1);
System.out.println(subtract);
// 3.乘
BigInteger multiply = bigInteger.multiply(bigInteger1);
System.out.println(multiply);
// 4.除
BigInteger divide = bigInteger.divide(bigInteger1);
System.out.println(divide);
}
}
2. BigDecimal 类
目前,我们学过精度最高的是double类型,但是,在实际开发时候,很有可能需要精度更高的类型,我们就需要用BigDecimal类;
public class BigDecimal_ {
public static void main(String[] args) {
BigDecimal bigDecimal = new BigDecimal("2.99999999999999999999999999999999");
System.out.println(bigDecimal);
// 在对BigDecimal类型的数据进行 加减乘除 时,不能直接+ - * /
// 要使用相应的方法
BigDecimal bigDecimal1 = new BigDecimal("1.63");
// 1.加
BigDecimal add = bigDecimal.add(bigDecimal1);
System.out.println(add);
// 2.减
BigDecimal subtract = bigDecimal.subtract(bigDecimal1);
System.out.println(subtract);
// 3.乘
BigDecimal multiply = bigDecimal.multiply(bigDecimal1);
System.out.println(multiply);
// 4.除
BigDecimal divide = bigDecimal.divide(bigDecimal1);//可能会抛出异常
// 在调用divide 方法时,指定精度即可
// 如果有无限循环小数,就会保留 分子 的精度
BigDecimal divide1 = bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);
System.out.println(divide1);
}
}