0
Good afternoon!
I have the following js function which returns the new cart values when the product quantity is changed:
function add(_quant, _preco, _total, _estoque) {
quantidade = parseInt($("#"+_quant).val());
estoque = parseInt($("#"+_estoque).val());
preco = parseFloat($("#"+_preco).val());
idCampo = _quant.substring(10, _quant.lenght);
novaQuantidade = quantidade + 1;
if(novaQuantidade <= estoque) {
if(novaQuantidade == 0) {
alert("Quatidade não por ser 0");
} else {
total = novaQuantidade * preco;
$("#"+_quant).val(novaQuantidade) ;
$("#"+_total).html(total.toFixed(2));
$.ajax({
type: "POST",
url: "_required/sessaoCarrinho.php",
data: {idProduto:idCampo, novaQuantidade: novaQuantidade},
dataType: 'json'
}).done(function(response){
subTotal = response['subTotal'];
$(".subTotal").html(subTotal.toFixed(2));
$(".totalCarrinhoTopo").html(subTotal.toFixed(2));
});
}
} else {
alert("Quantidade escolhida maior que estoque");
}
}
It turns out that this calculation is giving rounding error,
Possible duplicate: http://answall.com/questions/5642
– Woss
Forgive me, I do not see as duplicate because the answers present the explanation and do not present a solution! In this case, I am looking for the solution. If she even exists!
– Carlos Rocha
This reply seems to solve your problem, no?
– Woss
Not Anderson, the result was the same. That is, 37.71 * 2 = 63.41 instead of 63.42. Understand?
– Carlos Rocha
See on Ideone and in the Repl.it. Both gave 63,42 as response. In the browser continues 63,41?
– Woss
You opened my eyes to something else: I will check the php return of ajax. Hence, put the answer!
– Carlos Rocha
Yes, in PHP there is the same problem. See here.
– Woss
The problem was even in js. Problem solved Thank you! I just made total = novaQuantidity * parseFloat(price.toFixed(2));
– Carlos Rocha