BigDecimal一般在涉及到精確計算的時候會用到,下面是本身屢次錯誤使用BigDecimal的總結。java
//BigDecimal初始化 public static void testBigDecimalinit() { BigDecimal num1 = new BigDecimal(0.1); System.out.println("坑點1:num1="+num1);//坑點1:num1=0.1000000000000000055511151231257827021181583404541015625 BigDecimal num2 = new BigDecimal("0.1"); System.out.println("正確寫法:num2="+num2);//正確寫法:num2=0.1 }
if (num1.compareTo(BigDecimal.ZERO)>0) if (num1.compareTo(BigDecimal.ZERO)<0) if (num1.compareTo(BigDecimal.ZERO)==0)
//比較大小 public static void testBigDecimalCompareTo() { BigDecimal num1 = new BigDecimal("0.1"); BigDecimal num2 = new BigDecimal("0.100"); if (!num1.equals(num2)) { System.out.println("坑點1,用equals比較大小,num1="+num1+", num2="+num2+" 【不相等】"); } if (!(num1 == num2)) { System.out.println("坑點2,用==運算符比較大小,num1="+num1+", num2="+num2+" 【不相等】"); } if (num1.compareTo(num2) == 0) { System.out.println("正確比較大小,用compareTo,num1="+num1+", num2="+num2+" 【相等】"); } }
//BigDecimal除法 public static void testBigDecimalDivide() { BigDecimal num1 = new BigDecimal("1"); //坑點:Exception in thread "main" java.lang.ArithmeticException: Non-terminating decima l expansion; no exact representable decimal result. // System.out.println("坑點寫法1:"+num1.divide(new BigDecimal("3"))); // System.out.println("坑點寫法2:"+num1.divide(new BigDecimal("3")).setScale(2, BigDecimal.ROUND_DOWN)); System.out.println("正確寫法:"+num1.divide(new BigDecimal("3"), 2, BigDecimal.ROUND_HALF_DOWN)); }
結論:只有在divide的時候就設置好要精確的小數位數和舍入模式,才能避免出現沒法精確表達除不盡的問題。ide