Limit to two decimals in Jquery. Failed toFixed ()

1

I have a JQUERY code and I can not limit the decimals to two.

This is my code.

$('.cantidax').change(function()

{
    var precio = $(this).parents('td').prev().find('span.amount');
    var total = $(this).parents('td').next().find('span.amount');
    var valorPrecio = parseFloat(precio.text());
    var valorCantidad = parseFloat($(this).val());
    var PrecioxCantidad = valorPrecio * valorCantidad;
    var totales = parseFloat(total.text(PrecioxCantidad));
    var decimales = parseFloat(totales);

    console.log(typeof(valorPrecio));
    console.log(typeof(valorCantidad));
    console.log(typeof(PrecioxCantidad));
    console.log(typeof(totales));
    console.log(typeof(decimales));


});

The html code he works with is this

<td class="product-price">
   <span class="amount">{{$producto->precio}} €</span> 
</td>

 <td class="product-quantity">
<div class="quantity buttons_added">

<input type="number" size="4" class="input-text qty text cantidax"  value="" min="0" step="1" > 

 <a href="" class="btn btn-warning" >
      <i class="fa fa-refresh"></i>
</a>
 </div>
 </td>

<td class="product-subtotal">
       <span class="amount"></span> 
</td>

I have tried to get the type of each variable with console.log (typeof ()) and in all cases the result is number, but if I use toFixed () to delimit decimals, so that.

var decimales = parseFloat(totales).toFixed(2);

The decimals variable and the total variable is converted to NaN. Showing the results are these

       4503.91 
        14 
         63054.74 
        NaN 
        NaN

so the bug is in

var totales = parseFloat(total.text(PrecioxCantidad));      
var decimales = parseFloat(totales);

But I can not see it. What can be failing?

    
asked by KurodoAkabane 11.08.2016 в 19:26
source

1 answer

2

On the line:

var totales = parseFloat(total.text(PrecioxCantidad));      

the text method does not return the element text, but the element itself, when parameters are passed to it. To obtain the value, it should not have parameters

    
answered by 11.08.2016 в 19:52