public static void main(String[] args) {
//异常代码,除不尽导致
//BigDecimal divide = new BigDecimal(1).divide(new BigDecimal(0.3));
/*
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1690)
at Demo20.main(Demo20.java:63)
*/
//解决问题:进行除法运算时指定保留位数:scale
BigDecimal divide = new BigDecimal(1).divide(new BigDecimal(0.3),3,BigDecimal.ROUND_FLOOR);
System.out.println(divide); //3.333
}
标签:java,divide,下单,new,main,除不尽,BigDecimal
From: https://www.cnblogs.com/czzz/p/18057401