javascript parse float error

前端 未结 3 1808
花落未央
花落未央 2021-01-23 02:07

I am trying to get sum of rows of my table:

td1 val = $5,000.00; td2 val = $3000.00;

And I am using the following code:



        
3条回答
  •  旧时难觅i
    2021-01-23 02:20

    Try:

    var totalnum = 0;
    $('.num').each(function(){
       totalnum+= parseFloat($(this).html().substring(1).replace(',',''));
    });
    $('.total_num').html('$' + totalnum);
    

    This will remove the $ (or whatever currency symbol) from the beginning and all commas before doing the parseFloat and put it back for the total.

    Alternatively you could use the jQuery FormatCurrency plugin and do this:

    totalnum+= $(this).asNumber();
    

提交回复
热议问题