以下是显示的截图:
图中的数据相加是可以正常显示的,然后下面的两项在一起的时候,就出bug了。
其他的任意两项加都不会出现这种问题。
在Javabean中对数据采用的是BigDecimal的格式
javabean数据处理
public double getSubtotal() {
BigDecimal currPrice = new BigDecimal(book.getCurrPrice() + "");
BigDecimal count = new BigDecimal(quantity + "");
return currPrice.multiply(count).doubleValue();
}
在JS中对从JSP页面中抓取的数据作了先转化为text格式,然后Number(text),进行转换了。
js数据处理
function total(){
var total = 0;
$(":checkbox[name=checkboxBtn][checked=true]").each(function(){
var id = $(this).val();
var text = $("#" + id + "Subtotal").text();
total += Number(text);
});
$("#total").text(total);
}
JSP页面显示:
完全找不到bug点在哪儿?
这个问题我遇到过,属于JS浮点型数值运算的缺陷。给你一个方法。
function accAdd(arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m;
}
toFixed下,js有精度问题.
$("#total").text(total.toFixed(2));