1. 程式人生 > >BigDecimal去除末尾多餘的0

BigDecimal去除末尾多餘的0

Java有自帶的 stripTrailingZeros() 方法用於去除末尾多餘的0

 BigDecimal num = new BigDecimal("100.1000");
 BigDecimal numNoEndZero = num.stripTrailingZeros();  //numNoEndZero  :1E+2
 System.out.println(numNoEndZero.toString());

按上面的方法輸出結果,會顯示科學計數法,所以需要處理一下,解決方法:

 String numNoEndZeroStr = new BigDecimal("100.1000").stripTrailingZeros().toPlainString()
; //numNoEndZeroStr :100.1 System.out.println(numNoEndZeroStr);