1.保留double的指定位小数
public double round(double input, int digit) {
// 构造BigDecimal,注意用new BigDecimal(String)
BigDecimal bg = new BigDecimal(Double.toString(input));
return bg.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
}
2.将double数据转换成String型,使数据不以科学计数法的形式表示。
public String toPlainString(double d) {
BigDecimal b = new BigDecimal(Double.toString(d));
return b.toPlainString();
}
3.多位小数求余,类似于%运算符,不过支持浮点数
public double rem(double base, double divisor, int digit) {
BigDecimal bgBase = new BigDecimal(Double.toString(base)).setScale(digit, BigDecimal.ROUND_HALF_UP);
BigDecimal bgDivisor = new BigDecimal(Double.toString(divisor)).setScale(digit, BigDecimal.ROUND_HALF_UP);
return bgBase.remainder(bgDivisor).setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
}