javascript - How to sort a number with thousands separator -
i have tried sort number jquery datatables plug-in not working c# string number formats.
i have tried:
decimal value= 12345678.00 value..tostring("#,##.00"); value.tostring("##,##.##"); value.tostring("0,0.00", cultureinfo.invariantculture)
but no luck because of comma. if there no comma works fine or numbers same count working i.e.
01,121,372.01
02,002,009.22
11,222,222,33
if below not working
1,111,111.11
222,191.00
32,222.00
for datatables 1.10
datatables 1.10+ has formatted number detection , sorting abilities built- in, there no coding needed.
alternatively can set columns.type num-fmt
force specific data type.
see example below demonstration.
$(document).ready(function() { $('#example').datatable(); });
<!doctype html> <html> <head> <meta charset="iso-8859-1"> <link href="//cdn.datatables.net/1.10.7/css/jquery.datatables.min.css" rel="stylesheet" /> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="//cdn.datatables.net/1.10.7/js/jquery.datatables.min.js"></script> </head> <body> <table id="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 can use formatted numbers sorting plug-in.
you need include js file: //cdn.datatables.net/plug-ins/1.10.7/sorting/formatted-numbers.js , use code below set data type formatted number.
$('#example').datatable({ columndefs: [ { type: 'formatted-num', targets: 0 } ] });
Comments
Post a Comment