How to sort a number with thousands separator

ぐ巨炮叔叔 提交于 2019-12-11 03:31:58

问题


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");
value.ToString("##,##.##");
value.ToString("0,0.00", CultureInfo.InvariantCulture)

but no luck because of the comma. If there is no comma is works fine or all the numbers with same count also working i.e.

01,121,372.01

02,002,009.22

11,222,222,33

If it is as below then it is not working

1,111,111.11

222,191.00

32,222.00


回答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);
                    }
                }]



回答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>
<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 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 }
   ]
});


来源:https://stackoverflow.com/questions/31305504/how-to-sort-a-number-with-thousands-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!