Skip to content Skip to sidebar Skip to footer

How To Sort A Number With Thousands Separator

I have tried to sort the number with jQuery Datatables plug-in but is not working with C# string number formats. I have tried: decimal value= 12345678.00 value..ToString('#,##.00')

Solution 1:

I have done like this to overcome to this issue.

"aoColumnDefs": [ {
                    "aTargets": [3,4,6],
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        var$currencyCell = $(nTd);
                        var commaValue = $currencyCell.text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
                        $currencyCell.text(commaValue);
                    }
                }]

Solution 2:

For DataTables 1.10

DataTables 1.10+ has formatted number detection and sorting abilities built- in, there is no extra coding needed.

Alternatively you can set columns.type to num-fmt to force specific data type.

See the example below for demonstration.

$(document).ready(function() {
  $('#example').dataTable();

});



    

    

    
<!DOCTYPE html><html><head><metacharset="ISO-8859-1"><linkhref="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css"  /><linkhref="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"  /><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script></head><body><tableid="example"class="display"cellspacing="0"width="100%"><thead><tr><th>Name</th><th>Position</th><th>Office</th><th>Age</th><th>Start date</th><th>Salary</th></tr></thead><tfoot><tr><th>Name</th><th>Position</th><th>Office</th><th>Age</th><th>Start date</th><th>Salary</th></tr></tfoot><tbody><tr><td>Tiger Nixon</td><td>System Architect</td><td>Edinburgh</td><td>61</td><td>2011/04/25</td><td>111,111.11</td></tr><tr><td>Garrett Winters</td><td>Accountant</td><td>Tokyo</td><td>63</td><td>2011/07/25</td><td>222,191.00</td></tr><tr><td>Ashton Cox</td><td>Junior Technical Author</td><td>San Francisco</td><td>66</td><td>2009/01/12</td><td>32,222.00</td></tr></tbody></table></body></html>

For DataTables 1.9

For older DataTables 1.9 you can use Formatted numbers sorting plug-in.

You just need to include this JS file: //cdn.datatables.net/plug-ins/1.10.7/sorting/formatted-numbers.js and use the code below to set data type to formatted number.

$('#example').dataTable({
   columnDefs: [
     { type: 'formatted-num', targets: 0 }
   ]
});

Post a Comment for "How To Sort A Number With Thousands Separator"