0

tengo un problema, use {{number_format}} para convertir automaticamente decimales y todo bien... convierte a decimales pero cuando voy a hacer una operación obtengo error

$ NaN

TOTAL A PAGAR: $ NaN

este es el codigo...

    function updateList() {
        "use strict";
        var cart_items = "";
        var grandTotal = 0;
        for (let j = 0; j < cart.length; j++) {
            var total_price = 0;
            total_price = cart[j].qty * Number(cart[j].price);
            grandTotal += Number(total_price);
            cart_items += '<div class="p-4 bg-white rounded"><img class="rounded bp-2" src="' + cart[j].product_image + '"><div class="flex mb-6 mt-1 justify-between items-center"><div><h3 class="text-sm font-medium">' + cart[j].product_name + '</h3> <span class="text-xs text-gray-500">' + cart[j].subtitle + '</span></div></div><div class="flex mb-2 justify-between items-center"><h4 class="text-xl font-bold">' + currency + ' ' + total_price + '</h4> <a onclick="reduceQty(' + j + ')" class="py-2 px-3 bg-red-500 hover:bg-red-600 rounded-full text-xs text-white transition duration-200">-</a><h4 class="text-sm font-medium">' + cart[j].qty + '</h4> <a onclick="addQty(' + j + ')" class="py-2 px-3 bg-red-500 hover:bg-red-600 rounded-full text-xs text-white transition duration-200">+</a> <a class="py-2 px-3 bg-red-500 hover:bg-red-600 rounded-full text-xs text-white transition duration-200" onclick="removeFromCart(' + j + ')">X</a></div></div>';
        }
        cart_items += '<br> <h3 class="pl-4 pt-4 pr-4 font-bold">{{ __("TOTAL A PAGAR:") }} ' + currency + ' ' + grandTotal + '</h3>';
        $("#cart_items").html(cart_items);
    }

1 respuesta 1

0

Tu error es que estas realizando operaciones con String Javascript es delicado con eso, pero es sencillo.

Antes de realizar la operación matemática:

var numero='2';
console.log(numero*2);

Pasa tu valor a Número con paseFloat

var numero='2';
console.log(parseFloat(numero)*2);

Asi si es String lo pasas a Numero.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.