2017-01-29 1 views
-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 : –