Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/06/2013, 20:50
Avatar de destor77
destor77
 
Fecha de Ingreso: noviembre-2004
Ubicación: Gálvez, Santa Fe, Argentina
Mensajes: 2.654
Antigüedad: 19 años, 6 meses
Puntos: 43
problemas al sumar numeros con punto y comas

hola,
tengo el siguiente inconveniente, tengo una par de funciones que recorren un formulario sumando los valores que tiene los inputs de un formulario, estos valores al principio eran de este estilo:
1,50
1 500,60
22,99

todo andaba de maravilla, hasta que me pidieron que estos valores tenga el formarto:
1.50
1.500,60
22.99

y todo dejo de funcionarme y no entiendo porque :(
las funciones son estas:
Código Javascript:
Ver original
  1. function calcular_total(){
  2.     console.log('entro a calcular total');
  3.     var total = 0;
  4.     var subtotal = parseFloat($("#sub").text());
  5.     //subtotal = subtotal.replace(",",".");
  6.     console.log('subtotal: '+subtotal);
  7.     var desc = $("#descuento_gral").val();
  8.     console.log('descuento: '+desc);
  9.     if ($.isNumeric(desc)){
  10.         total = subtotal - (desc * subtotal) / 100;
  11.         console.log('total: '+total);
  12.         total = parseFloat(total).toFixed(2);
  13.         console.log('entro: '+parseFloat(total).toFixed(2));
  14.         $("#total").html(total);
  15.         $("#total_h").val(total);
  16.     }
  17.     else{
  18.         subtotal = parseFloat(subtotal).toFixed(2);
  19.         console.log('no entro: '+parseFloat(subtotal).toFixed(2));
  20.         $("#total").html(subtotal);
  21.         $("#total_h").val(subtotal);
  22.     }
  23. }
  24.  
  25.  
  26. function calcular_subtotal(){
  27.     console.log('subtotal');
  28.     var importe_subtotal = 0;
  29.     var precio;
  30.     $('.ui-sortable tr').each(
  31.         function(index, value) {
  32.             console.log("item: "+ $(this).find('input').eq(7).val());
  33.             precio = $(this).find('input').eq(7).val();
  34.  
  35.             importe_subtotal = importe_subtotal + parseFloat(precio);
  36.         }
  37.     );
  38.     importe_subtotal = parseFloat(importe_subtotal).toFixed(2);
  39.     console.log('sub: '+importe_subtotal);
  40.     $("#sub").html(importe_subtotal);
  41.     $("#sub_h").val(importe_subtotal);
  42. }

y la consola me devuelve:
Cita:
subtotal
item: 1.500,00
item: undefined
sub: NaN
entro a calcular total
subtotal: NaN
descuento:
no entro: NaN
porque con el nuevo formato no me deja sumar los valores?
intente cambiar los puntos por comas con replace pero me tira este error:
Cita:
Uncaught TypeError: Cannot call method 'replace' of undefined
estoy muy deconcertado con esto, alguien que me pueda dar una mano....
desde ya muchas gracais