如何用千位分隔符对数字进行排序

本文关键字:排序 数字 何用千 分隔符 | 更新日期: 2023-09-27 18:01:57

我尝试用jQuery数据表插件对数字进行排序,但不能使用c#字符串数字格式。

I have try:

decimal value= 12345678.00
value..ToString("#,##.00");
value.ToString("##,##.##");
value.ToString("0,0.00", CultureInfo.InvariantCulture)

但是没有运气,因为逗号。如果没有逗号,则正常工作,或者所有具有相同计数的数字也可以工作,即

01121372 .01点

02002009 .22

33

11222222年,

如果如下所示,则说明它不能工作

1111111 .11

222191

32222

如何用千位分隔符对数字进行排序

我这样做是为了克服这个问题。

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

1.10的datatable

DataTables 1.10+内置了格式化的数字检测和排序功能,不需要额外的编码。

也可以设置列。type to num-fmt强制指定数据类型

请参见下面的示例。

$(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>

对于旧的DataTables 1.9,您可以使用格式化数字排序插件。

您只需要包含这个JS文件://cdn.datatables.net/plug-ins/1.10.7/sorting/formatted-numbers.js并使用下面的代码将数据类型设置为格式化的数字。

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