在jsp页面运行一个sql语句,其中有一个字段是ROUND(CAST(A AS DOUBLE)/B,2),使用rs.getDouble(10)取出显示在页面后,很多值显示不正常(如:4.329999998等),有的就能正常显示四舍五入的结果(如:4.33)。更奇怪的是,如果复制结果拷贝到excel表中后,4.329999998就会显示为4.33,这是什么问题?该怎样解决?
调试欢乐多
public double round(double doubValue,int scale){
if(scale<0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(doubValue));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
我都是用这个方法的解决小数问题的.
解决方法:使用java.math.BigDecimal