I'm having a problem calculating the total. In the calculation you are not adding the decimals. For example, if a product is worth $150.24, by choosing "2" as the quantity, the calculation would be:
150.24 * 2 = 300.48. However as a result it shows 300.00.
Image (the results in red are the ones that should be given in the calculation):
here the table
<table class="table table-striped table-primary table-bordered" id="tableComprar">
<thead>
<tr>
<th class="text-center">Imagen</th>
<th class="text-center">Nombre</th>
<th class="text-center">P/unidad</th>
<th class="text-center">Cantidad</th>
<th class="text-center">Total</th>
</tr>
</thead>
<tbody>
@foreach (Libro item in Model)
{
idRow++;
<tr>
<th class="text-center"><img src="/@item.UrlImagen.Replace("\\", "/")" width="80" height="100" /></th>
<th class="text-center">@item.Nombre</th>
<th class="text-center">$ <input type="text" class="price" id="price-@idRow" value="@item.Precio" disabled /></th>
<th class="text-center">
<input type="number" onchange="Total(@idRow);" class="input-number" id="input-number-@idRow" value="1" min="1" max="@item.Cantidad" placeholder="1" />
</th>
<th class="text-center">$ <input type="text" class="total" id="total-@idRow" value="@item.Precio" disabled /></th>
</tr>
}
</tbody>
</table>
Here is the function that displays the result in the Total column
function Total(idRow) {
//Hay precios que son float en la bd, para que no de errores siempre convierto a float
var price = parseFloat($("#price-" + idRow).val());
var quantity = $("#input-number-" + idRow).val();
var result = parseFloat(price * quantity);
$("#total-" + idRow).val(result.toFixed(2));//.toFixed(2) muestre 2 decimales despues de la coma
finalPrice();
}
Here is the function that shows the result in Final Price
function finalPrice() {
var sum = 0;
$(".total").each(function () {
sum += +parseFloat($(this).val());
});
$("#finalPrice").val(sum.toFixed(2));//revisar no suma los decimales
}
It is necessary to replace the decimal separator with a point to operate, and replace it with a comma once the result is obtained to display it. It can be done in javascript function with
replace
.The same process would be followed to calculate the total.
SOLVED! Here I leave the 2 functions where I replace the comma with the point.