我想計算的價格不含稅實時這樣的:計算價格不含稅沒有給出確切的價格
價格包括taxe(priceTi)= 550
將TaxRate = 10 %
價格不含稅(priceTe)應該= 500
的問題是,我得到priceTe = 499.99999999999994
$(document).on('keyup', "#priceTi", function() {
var priceTe = $('#priceTe');
var taxRate = $('#taxRate');
var priceTi = $('#priceTi');
if (taxRate.val() != "") {
value = this.value.replace(/,/g, '.');
var tax = parseFloat((taxRate.val()/100) + 1) ;
$('#priceTe').val(parseFloat(value)/tax) ;
return false;
}
});
歡迎即浮點數的快樂痛苦。你可以使用'toFixed()'來解決這個問題。 –
http://stackoverflow.com/questions/588004/is-floating-point-math-broken – epascarello