-1

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;
    }
});
hous
  • 2,577
  • 2
  • 27
  • 66

1 Answers1

1

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)

Mike
  • 1,436
  • 9
  • 16