金額欄位顯示千分位

Posted by Leo Yang on 2023-11-22

開發網頁的時候常常會用boostrap datatable去呈現資料

如果金額的部分需要顯示千分位及顯示小數點後幾位可以參考下面的做法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
DetailTable = $('#exampleInQuriyData').DataTable(
{
"processing": true,
"paging": true,
"searching": true,
"bFilter": false,
"bDestroy": true,
"scrollY": "455px",
"scrollX": true,
"scrollCollapse": true,
"ajax": {
"url": 'apiurl',
"type": "POST",
"dataType": "json",
"data": {
"InQuiryStatus": 'R',
"ISINCode": mvISINCode
}
},
"columns": [
{ "data": "ReturnQuriyDate", "title": "回價時間", "sWidth": "210px", "className": "myCenter" },
{ "data": "ReturnPrice", "title": "回覆價格", "sWidth": "90px", "className": "myCenter" },
{ "data": "BS", "title": "B/S", "sWidth": "70px", "className": "myCenter" },
{ "data": "Currency", "title": "Currency", "sWidth": "60px", "className": "myCenter" },
{ "data": "Notional", "title": "Notional", "sWidth": "80px", "className": "myCenter" },
{ "data": "PriceModel", "title": "實際/參考價格", "sWidth": "90px", "className": "myCenter" },
{ "data": "InQuiryIdentifier", "title": "詢價編號", "sWidth": "120px", "className": "myCenter" }
],
//render定義千分位用什麼標點符號、小數點標點符號、小數點幾位
//target定義欄位index
columnDefs: [{
"render": $.fn.dataTable.render.number(',', '.', 2, '', ''),
"targets": [1, 4, 5],
}],
"lengthMenu": [[10, 50, -1], [10, 50, "All"]],
order: [[0, "desc"]]
});