Android double類型保留到小數點兩位,四舍五入
方法:
public double round(Double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = null == v ? new BigDecimal("0.0") : new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
scale就是需要傳入儲存小數點後幾位的值。