如何使用javascript / jquery将数字格式化为印度货币(在数据表中)

时间:2018-11-19 11:03:30

标签: javascript jquery datatables

我有一个数据表,该数据表是根据json数据更新的,我正在秘密地更新该数据表,但问题是我必须将金额格式化为印度货币,例如27227004,我希望它的值为2,72,22,004

$(document).ready(function (){
	
    var table_data = 
    	[
    	  [
    	    "CHEF BAKERS BROOKFIELD",
    	    "0",
    	    "2967629",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS ARAKERE",
    	    "0",
    	    "302542",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS AYYAPPA NGR",
    	    "0",
    	    "425433",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Bellandur",
    	    "0",
    	    "1278244",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS BAGMANE CBP",
    	    "0",
    	    "546345",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS CHANDAPURA",
    	    "0",
    	    "167527",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Doddanekkundi",
    	    "0",
    	    "490462",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS SIDDAPURA",
    	    "0",
    	    "461639",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS ECITY",
    	    "0",
    	    "699634",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS VYDEHI",
    	    "0",
    	    "459675",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS HARLUR ROAD",
    	    "0",
    	    "229458",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Hennur Main Road",
    	    "0",
    	    "211808",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS HSR LAYOUT",
    	    "0",
    	    "806820",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS KADUBEESANAHALLI",
    	    "0",
    	    "1108619",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS COFFEE BOARD",
    	    "0",
    	    "774311",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Kaggadasapura",
    	    "0",
    	    "280712",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Koramangala",
    	    "0",
    	    "345426",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS KASAVANAHALLI",
    	    "0",
    	    "415546",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Marathahalli 1",
    	    "0",
    	    "525344",
    	    "0"
    	  ],
    	  [
    	    "Chef bakers Marathahalli 2",
    	    "0",
    	    "507047",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Mahadevapura",
    	    "0",
    	    "2518170",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS BEL LAYOUT",
    	    "0",
    	    "404171",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS MG ROAD",
    	    "0",
    	    "450749",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS MANYATA TECH PARK",
    	    "0",
    	    "428558",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS NAGAWARA",
    	    "0",
    	    "444891",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS PRESTIGE SHANTHINIKETAN",
    	    "0",
    	    "576911",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS PRITECH",
    	    "0",
    	    "1269371",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS RR NAGAR",
    	    "0",
    	    "566927",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Kadugodi",
    	    "0",
    	    "1199501",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS SARJAPURA ROAD",
    	    "0",
    	    "457940",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS SINGASANDRA",
    	    "0",
    	    "245249",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS SPICE GARDEN",
    	    "0",
    	    "434369",
    	    "0"
    	  ],
    	  [
    	    "Chef Bakers Whitefield",
    	    "0",
    	    "1360325",
    	    "0"
    	  ],
    	  [
    	    "CHEF BAKERS YELAHANKA",
    	    "0",
    	    "1213855",
    	    "0"
    	  ]
    	]
    	       

    var table = $('#example').DataTable( {
        data: table_data,
        "scrollY": "200px",
	    "scrollCollapse": true,
    } );    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="example" class="table table-striped ">
    <thead>
        <tr>
            <th>Outlet</th>
            <th>MTD</th>
            <th>YTD</th>
            <th>Todays's Transaction</th>
            
        </tr>
    </thead>
   
</table>

我要做的是将数字格式化为印度货币

请任何人有任何想法指导我

5 个答案:

答案 0 :(得分:1)

您可以将其与toLocaleString

一起使用

function formatNumber()
{
    var number = parseFloat($("#test").val());

    console.log(number.toLocaleString('en-IN'));
}

formatNumber();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="test" value="2967629" />

<input type="button" onclick="formatNumber()" value="Format Number" />

答案 1 :(得分:1)

我准备了fiddle的印第安语言环境

var table = $('#example').DataTable({
    data: table_data,
    "scrollY": "200px",
    "scrollCollapse": true,
    "columnDefs": [{
      "targets": [2],
      "render": function(data, type, row) {
        return Number(data).toLocaleString('en-IN', {
          maximumFractionDigits: 2,
          style: 'currency',
          currency: 'INR'
        });
      }
    }]
  });

答案 2 :(得分:0)

您可以先格式化自己的数据,然后再将其传递到数据表,如下所示:

/**
 * formatNum - Formats given number with n decimals and separated by x length of sections
 * example: formatNum(1000000) -> gives : 100.000
 *
 * @param integer n: length of decimal
 * @param integer x: length of sections
 */
const formatNum = function (num, n, x) {
  const re = `\\d(?=(\\d{${x || 3}})+${n > 0 ? '\\.' : '$'})`
  // Convert string to number and do regex replace
  return Number(num).toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,')
}

$(document).ready(function () {

  var table_data =
    [
      [
        "CHEF BAKERS BROOKFIELD",
        "0",
        "2967629",
        "0"
      ],
      [
        "CHEF BAKERS ARAKERE",
        "0",
        "302542",
        "0"
      ],
      [
        "CHEF BAKERS AYYAPPA NGR",
        "0",
        "425433",
        "0"
      ],
      [
        "Chef Bakers Bellandur",
        "0",
        "1278244",
        "0"
      ],
      [
        "CHEF BAKERS BAGMANE CBP",
        "0",
        "546345",
        "0"
      ],
      [
        "CHEF BAKERS CHANDAPURA",
        "0",
        "167527",
        "0"
      ],
      [
        "Chef Bakers Doddanekkundi",
        "0",
        "490462",
        "0"
      ],
      [
        "CHEF BAKERS SIDDAPURA",
        "0",
        "461639",
        "0"
      ],
      [
        "CHEF BAKERS ECITY",
        "0",
        "699634",
        "0"
      ],
      [
        "CHEF BAKERS VYDEHI",
        "0",
        "459675",
        "0"
      ],
      [
        "CHEF BAKERS HARLUR ROAD",
        "0",
        "229458",
        "0"
      ],
      [
        "Chef Bakers Hennur Main Road",
        "0",
        "211808",
        "0"
      ],
      [
        "CHEF BAKERS HSR LAYOUT",
        "0",
        "806820",
        "0"
      ],
      [
        "CHEF BAKERS KADUBEESANAHALLI",
        "0",
        "1108619",
        "0"
      ],
      [
        "CHEF BAKERS COFFEE BOARD",
        "0",
        "774311",
        "0"
      ],
      [
        "Chef Bakers Kaggadasapura",
        "0",
        "280712",
        "0"
      ],
      [
        "Chef Bakers Koramangala",
        "0",
        "345426",
        "0"
      ],
      [
        "CHEF BAKERS KASAVANAHALLI",
        "0",
        "415546",
        "0"
      ],
      [
        "Chef Bakers Marathahalli 1",
        "0",
        "525344",
        "0"
      ],
      [
        "Chef bakers Marathahalli 2",
        "0",
        "507047",
        "0"
      ],
      [
        "Chef Bakers Mahadevapura",
        "0",
        "2518170",
        "0"
      ],
      [
        "CHEF BAKERS BEL LAYOUT",
        "0",
        "404171",
        "0"
      ],
      [
        "CHEF BAKERS MG ROAD",
        "0",
        "450749",
        "0"
      ],
      [
        "CHEF BAKERS MANYATA TECH PARK",
        "0",
        "428558",
        "0"
      ],
      [
        "CHEF BAKERS NAGAWARA",
        "0",
        "444891",
        "0"
      ],
      [
        "CHEF BAKERS PRESTIGE SHANTHINIKETAN",
        "0",
        "576911",
        "0"
      ],
      [
        "CHEF BAKERS PRITECH",
        "0",
        "1269371",
        "0"
      ],
      [
        "CHEF BAKERS RR NAGAR",
        "0",
        "566927",
        "0"
      ],
      [
        "Chef Bakers Kadugodi",
        "0",
        "1199501",
        "0"
      ],
      [
        "CHEF BAKERS SARJAPURA ROAD",
        "0",
        "457940",
        "0"
      ],
      [
        "CHEF BAKERS SINGASANDRA",
        "0",
        "245249",
        "0"
      ],
      [
        "CHEF BAKERS SPICE GARDEN",
        "0",
        "434369",
        "0"
      ],
      [
        "Chef Bakers Whitefield",
        "0",
        "1360325",
        "0"
      ],
      [
        "CHEF BAKERS YELAHANKA",
        "0",
        "1213855",
        "0"
      ]
    ].map(i => [i[0], i[1], formatNum(i[2]), i[3]])


  var table = $('#example').DataTable({
    data: table_data,
    "scrollY": "200px",
    "scrollCollapse": true,
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="example" class="table table-striped ">
    <thead>
        <tr>
            <th>Outlet</th>
            <th>MTD</th>
            <th>YTD</th>
            <th>Todays's Transaction</th>
            
        </tr>
    </thead>
   
</table>

编辑

这里是带有toLocaleString的版本(只需将格式设置函数更改为此):

const formatNum = function (num) {
  return (parseFloat(num)).toLocaleString('en-IN')
}

答案 3 :(得分:0)

尝试在特定列上使用datatable rander选项

chrome --apps 
  chrome-devtools://devtools/remote/serve_rev/@188492/devtools.html?remoteFrontend=true&dockSide=undocked
$(document).ready(function (){
	
    var table_data=[["CHEF BAKERS BROOKFIELD","0","2967629","0"],["CHEF BAKERS ARAKERE","0","302542","0"],["CHEF BAKERS AYYAPPA NGR","0","425433","0"],["Chef Bakers Bellandur","0","1278244","0"],["CHEF BAKERS BAGMANE CBP","0","546345","0"],["CHEF BAKERS CHANDAPURA","0","167527","0"],["Chef Bakers Doddanekkundi","0","490462","0"],["CHEF BAKERS SIDDAPURA","0","461639","0"],["CHEF BAKERS ECITY","0","699634","0"],["CHEF BAKERS VYDEHI","0","459675","0"],["CHEF BAKERS HARLUR ROAD","0","229458","0"],["Chef Bakers Hennur Main Road","0","211808","0"],["CHEF BAKERS HSR LAYOUT","0","806820","0"],["CHEF BAKERS KADUBEESANAHALLI","0","1108619","0"],["CHEF BAKERS COFFEE BOARD","0","774311","0"],["Chef Bakers Kaggadasapura","0","280712","0"],["Chef Bakers Koramangala","0","345426","0"],["CHEF BAKERS KASAVANAHALLI","0","415546","0"],["Chef Bakers Marathahalli 1","0","525344","0"],["Chef bakers Marathahalli 2","0","507047","0"],["Chef Bakers Mahadevapura","0","2518170","0"],["CHEF BAKERS BEL LAYOUT","0","404171","0"],["CHEF BAKERS MG ROAD","0","450749","0"],["CHEF BAKERS MANYATA TECH PARK","0","428558","0"],["CHEF BAKERS NAGAWARA","0","444891","0"],["CHEF BAKERS PRESTIGE SHANTHINIKETAN","0","576911","0"],["CHEF BAKERS PRITECH","0","1269371","0"],["CHEF BAKERS RR NAGAR","0","566927","0"],["Chef Bakers Kadugodi","0","1199501","0"],["CHEF BAKERS SARJAPURA ROAD","0","457940","0"],["CHEF BAKERS SINGASANDRA","0","245249","0"],["CHEF BAKERS SPICE GARDEN","0","434369","0"],["Chef Bakers Whitefield","0","1360325","0"],["CHEF BAKERS YELAHANKA","0","1213855","0"]];

    var table = $('#example').DataTable( {
        data: table_data,
        "scrollY": "200px",
	    "scrollCollapse": true,
       "columns": [
            { mData: '0' } ,
            { mData: '1' },
            { mData: '2',  render: function (data, type, row, meta) { 
            return parseFloat(data).toLocaleString('en-IN');
            }
            },
            { mData: '3' }
        ]
    } );    
});

答案 4 :(得分:0)

$(document).ready(function() {
    var table = $('#example').DataTable({
        data: table_data,
        columns: [
            { data: 'Outlet' },
            { data: 'MTD' },
            { data: 'YTD', render: function (data, type, row) {
                var data = parseFloat(data);
                return data.toLocaleString('en-IN');
            } },
            { data: 'Todays\'s Transaction' },
        ],
        "scrollY": "200px",
        "scrollCollapse": true,
    } );
} );