【ArithmeticException】: Non-terminating decimal expansion; no exact representable decimal result。

原來JAVA中若是用BigDecimal作除法的時候必定要在divide方法中傳遞第二個參數,定義精確到小數點後幾位,不然在不整除的狀況下,結果是無限循環小數時,就會拋出以上異常。java

解決方法:app

foo.divide(bar, 2, BigDecimal.ROUND_HALF_UP);
注意這個divide方法有兩個重載的方法,一個是傳兩個參數的,一個是傳三個參數的:

兩個參數的方法:

@param divisor value by which this {@code BigDecimal} is to be divided. 傳入除數

@param roundingMode rounding mode to apply. 傳入round的模式

三個參數的方法:

@param divisor value by which this {@code BigDecimal} is to be divided. 傳入除數
@param scale scale of the {@code BigDecimal} quotient to be returned. 傳入精度
@param roundingMode rounding mode to apply. 傳入round的模式
相關文章
相關標籤/搜索