标签:details double present stat ring 构造函数 mat article cal
BigDecimal做除法时如果出现除不尽(循环小数)的情况,会抛异常:
BigDecimal a = new BigDecimal("1"); System.out.println(a.divide(new BigDecimal(3)));
异常如下:
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1693)
System.out.println(a.divide(new BigDecimal(30), 2,BigDecimal.ROUND_HALF_UP));
public static void main(String[] args) { BigDecimal value1 =new BigDecimal(1024.511); System.out.println("value1: " + value1); BigDecimal value2 = new BigDecimal("1024.511"); System.out.println("value2: " + value2); }
按照我们的预想,value1输出的应该是10.511,可是实际的值却不是这样,如下:
value1: 10.510999999999999232613845379091799259185791015625
value2: 10.511
标签:details double present stat ring 构造函数 mat article cal
原文地址:https://www.cnblogs.com/east7/p/11706738.html