2017-01-29 9 views
-1

は、しかし私は、そのが正しく計算しない1列の合計を取得するには、このコードを使用し追加テーブルのカラムは

予想される出力:13,500.00 現在出力:13

私はこの問題は形式だと思います私はそれに対処する方法を知りません。

<table id="sum_table" width="300" border="1"> 
     <tr class="titlerow"> 
      <td>Col 1</td> 
     </tr> 
     <tr> 
      <td class="amount">6,000.00</td> 
     </tr> 
     <tr> 
      <td class="amount">1,500.00</td> 
     </tr> 
     <tr> 
      <td class="amount">6,000.00</td> 
     </tr> 
    </table> 
      <div id="total"></div> 
    <script> 
    var theTotal = 0; 
    $(".amount").each(function() { 
     theTotal += parseInt($(this).html()); 
    }); 
    $("#total").html(theTotal); 
    </script> 

答えて

2

,を文字列から置き換えて、文字列を正しく解析する必要があります。

theTotal += Number($(this).html().replace(/,/g,'')); 
// or remove all character except digit and dot 
theTotal += Number($(this).html().replace(/[^\d.]/g,'')); 

UPDATE:後文字列に合計を変換することによって、通貨フォーマットに変換します。

$("#total").html(theTotal.toString().replace(/\d(?=(?:\d{3})+$)/g,'$&,')) 
// or with 2 decimal point 
$("#total").html(theTotal.toFixed(2).replace(/\d(?=(?:\d{3})+\.)/g,'$&,')) 
+0

あなたの回答は正しいですが、どのように通貨形式で出力しますか? 。 –

+0

は:(機能していない –

+0

@ShermaineChaingan: –

関連する問題