2016-12-15 06:08

# BigDecimal的加减乘除没有效果，

public class Myutil {
public static double add(double value1, double value2) {
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
}

``````public static double sub(double value1, double value2) {
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
return b1.subtract(b2).doubleValue();
}

public static double mul(double value1, double value2) {
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
return b1.multiply(b2).doubleValue();
}

public static double div(double value1, double value2, int scale)
throws IllegalAccessException {
if (scale < 0) {
throw new IllegalAccessException("精确度不能小于0");
}
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
return b1.divide(b2, scale).doubleValue();
}
``````

}

public void test1() throws IllegalAccessException{
System.out.println(Myutil.sub(1.0, 0.42));
System.out.println(Myutil.mul(4.015, 100));
System.out.println(Myutil.div(303.1, 1000, 4));
}

``````结果
``````

0.5800000000000001
401.49999999999994
0.06999999999999999
0.30310000000000004

• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答

#### 1条回答默认 最新

• 大脸猫丶 2016-12-15 06:20
最佳回答

return new BigDecimal((double)(a+b)).setScale(2, BigDecimal.ROUND_HALF_UP);
@caozhy大大说要四舍五入.

评论
解决 无用
打赏 举报