0

I have code that calculate value in HTML table. It works if I use .(dot) but not when I use ,(comma)

also my numbers have space if number bigger than 1000 example 2 476,98

how can I make it working as I need?

Javascript:

var totals = [0, 0, 0, 0, 0, 0];
$(document).ready(function () {
    var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");
    $dataRows.each(function () {
        $(this).find('.rowDataSd').each(function (i) {
            totals[i] += parseFloat($(this).html());
        });
    });
    $("#sum_table td.totalCol").each(function (i) {
        $(this).html("kopā: " + totals[i].toFixed(2));
    });
});

And here its on jsfiddle

AsgarAli
  • 2,201
  • 1
  • 20
  • 32
Ingus
  • 1,026
  • 12
  • 34
  • `.replace(/ /g, "").replace(/,/g,".")` – ASDFGerte Nov 03 '16 at 12:00
  • `parseFloat` is used to transform a string that looks like a number to a number - ie. `2476.98`. With `,` it will transform it to `2476`. To get around that plainly replace `,` to `.` and space to empty, ie - `parseFloat($(this).html().replace(",",".").replace(" ", ""))` – eithed Nov 03 '16 at 12:00
  • 1
    Possible duplicate of [Convert String with Dot or Comma as decimal separator to number in JavaScript](http://stackoverflow.com/questions/7431833/convert-string-with-dot-or-comma-as-decimal-separator-to-number-in-javascript) – Álvaro González Nov 03 '16 at 12:02
  • @canisminor afaik `replace` just replaces the first occurrence when provided with string-only arguments. If a number like `1 234 567,89` is possible, regex with global modifier should do the trick `/ /g`. But my method also fails to accomodate for the comment in the link for the duplicate, where there are multiple commas :) – ASDFGerte Nov 03 '16 at 12:02
  • Thanks everyone who helped :) – Ingus Nov 03 '16 at 13:00

3 Answers3

3
<script>
    var totals = [0, 0, 0, 0, 0, 0];
    $(document).ready(function () {
        var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");
        $dataRows.each(function () {
            $(this).find('.rowDataSd').each(function (i) {
                totals[i] += parseFloat($(this).text().replace(/[^\d.-]/g, ''));
            });
        });
        $("#sum_table td.totalCol").each(function (i) {
            $(this).text("kopā: " + totals[i].toFixed(2));
        });
    });
</script>
AsgarAli
  • 2,201
  • 1
  • 20
  • 32
2

You need to detect , and replace them by . and space by nothing "".

On this line

totals[i] += parseFloat($(this).html());

replace it by this:

totals[i] += parseFloat($(this).text().replace(",",".").replace(/ /g, ""));

the / /g is a regular expression to detect multiple spaces on a string. example "1 932 123" will convert to "1932123"

sandrina-p
  • 3,794
  • 8
  • 32
  • 60
1

var totals=[0,0,0,0];
$(document).ready(function(){

    var $dataRows=$("#sum_table tr:not('.totalColumn, .titlerow')");
    
    $dataRows.each(function() {
        $(this).find('.rowDataSd').each(function(i){        
            totals[i]+=parseFloat( $(this).html().replace(/[^0-9.]/g, ''));
        });
    });
    $("#sum_table td.totalCol").each(function(i){ 
 

    
        $(this).html("kopā: "+totals[i].toFixed(2));
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sum_table" border="1">
     <tr class="titlerow">
      <td align="center">plan1
     </tr>
     <tr class="titlerow">
      <td align="center">EUR     
     </tr>
     <tbody>
         <tr>  
       <td class="rowDataSd" align="right" nowrap>2 358,94
         </tr>
         <tr>
         <td class="rowDataSd" align="right" nowrap>3
         </tr>
         <tr> 
             <td class="totalCol"></td>
        </tr>  
</tbody></table>
LF00
  • 27,015
  • 29
  • 156
  • 295