jquery数据表禁止使用科学表示法

o4hqfura  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(397)

我正在使用datatables呈现一个表并显示api中的数据。

<script type="text/javascript">
$.fn.dataTable.ext.errMode = 'throw';
$(document).ready(function (){
    var table = $('#example').DataTable({

        'ajax': {
            'url': '/api/books',
            'dataSrc': '',

        },
      "language": {
        "emptyTable": "Nothing to show",
    },
      "columns": [
            { "data": "price", render: $.fn.dataTable.render.number(',', '.', 8, ''), "width":"40%"  },
            { "data": "amount", render: $.fn.dataTable.render.number(',', '.', 8, '') },
            { "data": "sum", render: $.fn.dataTable.render.number(',', '.', 8, '') },

        ],
       "scrollY":        "300px",
       "scrollCollapse": true,
       "autoWidth": false,
       "lengthChange": false,
       "ordering": false,
       "pageLengt"': 50,
       "bFilter": false,
       "bPaginate": false,
       "bInfo": false,
       "bSort": false,
       "createdRow": function( row, data, dataIndex){

                [...more code here...]
});

</script>

此代码工作正常,但任何小于0.0000100的数字都使用科学记数法显示。有没有办法抑制它?谢谢你的帮助

klsxnrf1

klsxnrf11#

您可以使用javascript Intl.NumberFormat datatables列渲染器中的对象:

Intl.NumberFormat('en', { maximumSignificantDigits: 15 }).format(1234.00000000123);

下面是一个演示:

var dataSet = [
    {
      "name": "Tiger Nixon",
      "amount": 1234.00000000123
    }
  ];

$(document).ready(function() {

var table = $('#example').DataTable( {
  data: dataSet,
  columns: [
    { 
      title: "Name", 
      data: "name" 
    },
    {
      title: "Amount", 
      data: "amount",
      render: function ( data, type, row, meta ) {
        if (type === 'display') {
          return Intl.NumberFormat('en', { maximumSignificantDigits: 15 }).format(data);
        } else {
          return data;
        }
      }
    }
  ]

} );

} );
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Demo</title>
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
  <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">

</head>

<body>

<div style="margin: 20px;">

    <table id="example" class="display dataTable cell-border" style="width:100%">
    </table>

</div>

</body>
</html>

问题中使用的插件使用javascript parseFloat() 函数,如您所见,如果值足够小,它会将值转换为科学符号-如您给出的示例:

parseFloat(0.00000099) -> 9.9e-7

相关问题