我在此腳本中出現錯誤。計算系統的工作原理,但它有一個錯誤:如果我calcultate 49.95 + 149.95結果是199.89999995總計以.999995結尾顯示價格
$(document).ready(function() {
$("#total").val("55.95");
$("#amount").val("512MB");
$("#amount1").val("512MB");
$("#amountb").val("10GB");
$("#amount2").val("10GB");
$(function() {
var ram = {
0: "512MB",
1: "1GB",
2: "2GB",
3: "4GB",
4: "8GB",
5: "16GB",
6: "32GB",
};
var pram = {
0: "49.95",
1: "89.95",
2: "149.95",
3: "32",
4: "64",
5: "128",
6: "256",
}
var hdd = {
0: "10GB",
1: "15GB",
2: "20GB",
3: "25GB",
4: "30GB",
5: "35GB",
};
var phdd = {
0: "49.95",
1: "99.95",
2: "100.95",
3: "49.14",
4: "28",
5: "40",
}
$("#slider").slider({
value: "0",
min: 0,
max: 6,
step: 0,
slide: function(event, ui) {
$("#price").val(pram[ui.value]);
$("#amount").val(ram[ui.value]);
$("#amount1").val(ram[ui.value]);
var aaa = $("#price").val();
var bbb = $("#priceb").val();
$("#total").val(+aaa + +bbb);
}
});
$("#sliderb").slider({
value: "0",
min: 0,
max: 5,
step: 1,
slide: function(event, ui) {
$("#priceb").val(phdd[ui.value]);
$("#amountb").val(hdd[ui.value]);
$("#amount2").val(hdd[ui.value]);
var aaa = $("#price").val();
var bbb = $("#priceb").val();
$("#total").val(+aaa + +bbb);
}
});
$("#price").val('$' + $("#slider").slider("value"));
$("#priceb").val('$' + $("#slider").slider("value"));
$("#price").val("4");
$("#priceb").val("3");
}
);
});
,其結果是199.9 我不知道該怎麼辦。請幫助我
? – 2013-03-21 09:50:18
不要對貨幣數據使用浮點值。以美分爲單位保留價格並相應地爲演示文稿格式化數字。浮動總是有一個四捨五入錯誤:http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken。 – 2013-03-21 09:50:36
,我該如何改變? – Schito9X 2013-03-21 09:56:55