hous - 1 year ago 63
jQuery Question

Calculate price excluding tax doesn't give the exact price

I'd like to calculate price excluding tax in real time like this :

Price included taxe (priceTi) = 550

TaxRate = 10 %

Price excluding tax (priceTe) should be = 500

The problem is that I get 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;
}
});
``````

By using .toFixed(2) you can get the desired result.

``````\$(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 ).toFixed(2) ;

return false;
}
});
``````

You can also round the to the nearest integer by using round. .49 will be rounded down, .5 will be rounded up. This function is `Math.round(int)`

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download