大数值运算类
1. Biginter
用途:BigInteger可以表示比Integer更大的数字,是整数类型
public class Test01{ public static void main(String[] args){ //创建对象并赋值 BigInteger bigInteger1=new BigInteger("12345678901234"); BigInteger bigInteger2=new BigInteger("12345678956789"); //做加法运算 BigInteger add=bigInteger1.add(bigInteger2); System.out.println(add); //做减法 BigInteger subtract=bigInteger1.subtract(bigInteger2); System.out.println(subtract); //做乘法 BigInteger multiply=bigInteger1.multiply(bigInteger2); System.out.println(multiply); //做除法 BigInteger divide=bigInteger1.divide(bigInteger2); System.out.println(divide); } }
2. BigDecemal
用途:作用于浮点数的计算,小数计算做除法时,一定要设置保留几位小数以及取舍方法,是向上进去还是向下又或者四舍五入,一定要写上。
public class Test01{ public static void main(String[] args){ /** 浮点型大值运算 */ BigDecimal bigDecimal1=new BigDecimal("0.5"); BigDecimal bigDecimal2=new BigDecimal("0.4"); BigDecimal add = bigDecimal1.add(bigDecimal2);//加法 System.out.println(add); BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);//减法 System.out.println(subtract); BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);//乘法 System.out.println(multiply); BigDecimal divide = bigDecimal1.divide(bigDecimal2);//除法 System.out.println(divide); } }
注意:
public class Test01{ public static void main(String[] args){ /** * 注意:小数除法一定要设置保留几位小数 和 进制模式 */ BigDecimal big1 = new BigDecimal("10"); BigDecimal big2 = new BigDecimal("3"); //除法 BigDecimal divide = big1.divide(big2, 2, BigDecimal.ROUND_HALF_UP); System.out.println(divide); } }