1 package day2_15; 2 3 import org.junit.Test; 4 5 import java.math.BigDecimal; 6 import java.math.BigInteger; 7 8 /** 9 * @Author Tianhao 10 * @create 2021-02-15-16:44 11 */ 12 public class BigIntegerAndBigDecimal { 13 14 @Test 15 public void test() { 16 //注意:私有构造器,是会报错的 private BigInteger(long val) 17 //调用公共构造器 public BigInteger(String val) 18 BigInteger bi = new BigInteger("4627656745686875679568678573"); 19 System.out.println(bi); //4627656745686875679568678573 20 21 BigDecimal bd = new BigDecimal("123456.893"); 22 System.out.println(bd);//123456.893 23 24 BigDecimal bd2 = new BigDecimal("11"); 25 26 ////bd除以bd2 报错:java.lang.ArithmeticException: 27 // Non-terminating decimal expansion; no exact representable decimal result. 28 //因为结果是无限循环的小数,没有准确的结果可以表示,所以必须指定四舍五入才行 29 //System.out.println(bd.divide(bd2)); 30 31 //bd除以bd2,四舍五入,默认保留3位小数 32 System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP));//11223.354 33 //bd除以bd2,四舍五入,指定精度,保留15位小数 34 System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_HALF_UP));//11223.353909090909091 35 36 37 } 38 }