Totalling number columns including commas (e.g. 1,234.56)

Totalling number columns including commas (e.g. 1,234.56)

stevergillstevergill Posts: 4Questions: 0Answers: 0
edited January 2013 in General
Hi

I'm having trouble working this one out. Its miles easier for me to format the source data for datatables with commas i.e. 1,234.56 instead of 1234.56.

However when I try and total I get NaN (not a number). Is there a way I can get the totals to work whilst still keeping the comma formatting?

Test Case here, showing the Retail column not totalling and the percentage column as well: http://live.datatables.net/igamim/2/edit#javascript,html,live

Many thanks!

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Not really something DataTables will provide (it will just give you your data), but you can remove the non-numeric data with a simple regex and then use `parseFloat()` to covert it to a number for summing.

    Allan
  • psspss Posts: 19Questions: 0Answers: 0
    Hi Stevergill,
    Probably you are looking for something like explained here:

    http://www.resolved-technologies.com/javascript/jquery-datatables-2/
    http://www.resolved-technologies.com/wp-content/themes/PersonalPress/js/dataTablesExample.js

    I too was looking for the same solution some time ago and found above link useful.

    Cheers.
  • stevergillstevergill Posts: 4Questions: 0Answers: 0
    I'll have a look, thanks Allan! As an alternative, if I was to provide the source data as 1234.56, is there a simple way to format both the data and the total with commas? I tried something here: http://live.datatables.net/uparux/3/edit but I wasn't sure where to put "formatted-num", so I tried a function formatNumber but as you can see the total still doesn't like it.

    Thanks!
  • stevergillstevergill Posts: 4Questions: 0Answers: 0
    Hi pss, thanks for the links - I'll have a look, much appreciated!
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Currently you can use this plug-in to get the data for a column: http://datatables.net/plug-ins/api#fnGetColumnData - then just sum the array.

    DataTables 1.10 will provide a similar API method built in.

    Allan
  • stevergillstevergill Posts: 4Questions: 0Answers: 0
    Hi Allan, can you tell me what I'm doing wrong with the formatted numbers plugin? I've set it up here: http://live.datatables.net/ukaheh/2/edit and set sType to be formatted-num but it doesn't change the raw 1234.56 to display as 1,234.56

    Many thanks
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Its not a display plug-in - its a sorting plug-in. So it effects the sorting, not the display.

    If you want to actually add number formatted you'd do so using an mRender function.

    Allan
This discussion has been closed.