-2

I'm using number_format($number,2,",","."). An example number is in this format: 8.726,36

I did sum of rows with 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(totals[i].toFixed(2).replace(".",",")+ " kn");
    });
});

But total is not good (blue row).

Photo

How can I use multiple .replace(",",".")?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
proofzy
  • 627
  • 1
  • 12
  • 23
  • 1
    `.replace(/,/g,'.');`? – putvande Jan 16 '17 at 10:23
  • 1
    You clearly know that JavaScript always uses `.` for decimal, and you clearly know how to replace characters, so what's the question? Your `totals[i] += parseFloat( $(this).html());` line ignores JS's number format and feeds it values like `8.726,36` expecting it to realize that's a number in the thousands. It won't magically do that, *you* have to do it, the same way you did the converse when displaying at the end. – T.J. Crowder Jan 16 '17 at 10:25
  • Is your question how you should use . instead of , as a thousands separator? This has to do with localisation. Does this question help you? http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Kokodoko Jan 16 '17 at 10:26

1 Answers1

1

Parsing numbers in and out of human-readable format is a terrible idea. What if one day your team decides to use a different format, like 1'234'567.89? You're stuffed!

Instead, generate something like:

<span class="number" data-value="1234567.89">1.234.567,89</span>

That way you can access .data('value') to get the actual value of the number, consistently, without worry for the future.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592