0

I have the following function as part of my datatables implementation. It works well to create a total on the bottom of the table for column 7.

                        "drawCallback": function () {
                        var api = this.api();
                        var column = api.column(7);
                        $(column.footer()).html(
                            "Total:   " + api.column(7, { page: 'current' }).data().sum()
                        );

My client wants to format the number with a comma thousand separator. On the fields directly I use the built in function.

render: $.fn.dataTable.render.number(',', '.', 2, '')

How do you apply similar to the HTML builder for the footer total.

Steve Newton
  • 1,046
  • 1
  • 11
  • 28
  • Does this answer your question? [Formatting a number with exactly two decimals in JavaScript](https://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript) – andrewJames May 03 '22 at 21:30
  • In all honesty, no as decimal places was not the question. – Steve Newton May 03 '22 at 21:33
  • The linked answers include `Intl.NumberFormat` - is that not what you need? Doesn't the `2` in `$.fn.dataTable.render.number(',', '.', 2, '')` represent the number of decimal places to show? Can you therefore not use [`{ maximumSignificantDigits: 2 }`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) or whatever you may need? That would seem to be a good fit for this question. – andrewJames May 03 '22 at 21:42
  • Some other resources which may also be helpful: [How to print a number with commas as thousands separators in JavaScript](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript/17663871#17663871) – andrewJames May 04 '22 at 13:21

1 Answers1

1

Why not use Intl.NumberFormat?

new Intl.NumberFormat('en-US').format(api.column(7, { page: 'current' }).data().sum())
cheesyMan
  • 1,494
  • 1
  • 4
  • 13