-3

I have an HTML table which have some data into it as a number. What I want to do is to format the numbers; currently I have 123456 and I want it format like 1,23,456.

I have tried with Java in my back end with Java number format but in my query I am using round(sum(amount)) so not able to get that format on UI because I am manipulating the json with JavaScript only. I want to achieve this by JavaScript.

Here is my code

 data= [
  {
    "amount": 291589,
    "billdate": "2018-08-01",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58337,
    "billdate": "2018-08-01",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 65970,
    "billdate": "2018-08-01",
    "outlet": "KOLAR"
  },
  {
    "amount": 296125,
    "billdate": "2018-08-02",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 56545,
    "billdate": "2018-08-02",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 72213,
    "billdate": "2018-08-02",
    "outlet": "KOLAR"
  },
  {
    "amount": 346605,
    "billdate": "2018-08-03",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 62459,
    "billdate": "2018-08-03",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 65248,
    "billdate": "2018-08-03",
    "outlet": "KOLAR"
  },
  {
    "amount": 518212,
    "billdate": "2018-08-04",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 104801,
    "billdate": "2018-08-04",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 138151,
    "billdate": "2018-08-04",
    "outlet": "KOLAR"
  },
  {
    "amount": 628358,
    "billdate": "2018-08-05",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 115223,
    "billdate": "2018-08-05",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 134107,
    "billdate": "2018-08-05",
    "outlet": "KOLAR"
  },
  {
    "amount": 177866,
    "billdate": "2018-08-06",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 66095,
    "billdate": "2018-08-06",
    "outlet": "KOLAR"
  },
  {
    "amount": 284069,
    "billdate": "2018-08-07",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58789,
    "billdate": "2018-08-07",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 67886,
    "billdate": "2018-08-07",
    "outlet": "KOLAR"
  },
  {
    "amount": 313128,
    "billdate": "2018-08-08",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 59939,
    "billdate": "2018-08-08",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 68558,
    "billdate": "2018-08-08",
    "outlet": "KOLAR"
  }
]

let formatData = function (data) {

                let billdates = [];
                let outlets = [];
                data.forEach(element => {
                    if (billdates.indexOf(element.billdate) == -1) {
                        billdates.push(element.billdate);
                    }
                    if (outlets.indexOf(element.outlet) == -1) {
                        outlets.push(element.outlet);
                    }
                });
                return {
                    data: data,
                    billdates: billdates,
                    outlets: outlets,

                };
            };



            let renderTable = function (data) {
                billdates = data.billdates;
                outlets = data.outlets;
                data = data.data;
                let tbl = document.getElementById("tbl");
                let table = document.createElement("table");
                let thead = document.createElement("thead");
                let headerRow = document.createElement("tr");
                let th = document.createElement("th");
                th.innerHTML = "Bill___Date";
                th.classList.add("text-center");
                headerRow.appendChild(th);
                let grandTotal = 0;
                let outletWiseTotal = {};
                th = document.createElement("th");
                th.innerHTML = "Total";
               th.classList.add("text-center");
                headerRow.appendChild(th);
                outlets.forEach(element => {
                    th = document.createElement("th");
                    th.innerHTML = element;
                   th.classList.add("text-center");
                    headerRow.appendChild(th);
                    outletWiseTotal[element] = 0;
                    data.forEach(el => {
                        if (el.outlet == element) {
                            outletWiseTotal[element] += parseInt(el.amount);
                        }
                    });
                    grandTotal += outletWiseTotal[element];
                });

                thead.appendChild(headerRow);
                headerRow = document.createElement("tr");
                th = document.createElement("th");
                th.innerHTML = "Total";
              th.classList.add("text-center");

                headerRow.appendChild(th);

                outlets.forEach(element => {
                    th = document.createElement("th");
                    th.innerHTML = outletWiseTotal[element];
                    th.classList.add("text-right");   //ol totals
                    headerRow.appendChild(th);
                });
                th = document.createElement("th");
                th.innerHTML = grandTotal;
                 th.classList.add("text-right");  // grand total
         /*  console.log(grandTotal);*/
               // headerRow.appendChild(th);
           headerRow.insertBefore(th , headerRow.children[1] );
                thead.appendChild(headerRow);
                table.appendChild(thead);

                let tbody = document.createElement("tbody");


                billdates.forEach(element => {
                    let row = document.createElement("tr");  
                    td = document.createElement("td");
                    td.innerHTML = element;
                    row.appendChild(td);
                    let total = 0;
                    outlets.forEach(outlet => {
                        let el = 0;
                        data.forEach(d => {
                            if (d.billdate == element && d.outlet == outlet) {
                                total += parseInt(d.amount);
                                el = d.amount;
                            }
                        });
                        td = document.createElement("td");
                        td.innerHTML = el;
                        td.classList.add("text-right"); //oldata
                        row.appendChild(td);
                    });
                    /*console.log("row is : " , row.children )*/
                    td = document.createElement("td");
                    td.innerHTML = total;
                    td.classList.add("text-right"); //column total
                   // row.appendChild(td);
                    row.insertBefore(td , row.children[1] );
                    tbody.appendChild(row);
                });

                table.appendChild(tbody);

                tbl.innerHTML = "";
                tbl.appendChild(table);
                table.classList.add("table");
                table.classList.add("table-striped");
                table.classList.add("table-bordered");
            }
            let formatedData = formatData(data);
            renderTable(formatedData);

data= [
  {
    "amount": 291589,
    "billdate": "2018-08-01",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58337,
    "billdate": "2018-08-01",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 65970,
    "billdate": "2018-08-01",
    "outlet": "KOLAR"
  },
  {
    "amount": 296125,
    "billdate": "2018-08-02",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 56545,
    "billdate": "2018-08-02",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 72213,
    "billdate": "2018-08-02",
    "outlet": "KOLAR"
  },
  {
    "amount": 346605,
    "billdate": "2018-08-03",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 62459,
    "billdate": "2018-08-03",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 65248,
    "billdate": "2018-08-03",
    "outlet": "KOLAR"
  },
  {
    "amount": 518212,
    "billdate": "2018-08-04",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 104801,
    "billdate": "2018-08-04",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 138151,
    "billdate": "2018-08-04",
    "outlet": "KOLAR"
  },
  {
    "amount": 628358,
    "billdate": "2018-08-05",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 115223,
    "billdate": "2018-08-05",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 134107,
    "billdate": "2018-08-05",
    "outlet": "KOLAR"
  },
  {
    "amount": 177866,
    "billdate": "2018-08-06",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 66095,
    "billdate": "2018-08-06",
    "outlet": "KOLAR"
  },
  {
    "amount": 284069,
    "billdate": "2018-08-07",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 58789,
    "billdate": "2018-08-07",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 67886,
    "billdate": "2018-08-07",
    "outlet": "KOLAR"
  },
  {
    "amount": 313128,
    "billdate": "2018-08-08",
    "outlet": "JAYANAGAR"
  },
  {
    "amount": 59939,
    "billdate": "2018-08-08",
    "outlet": "MALLESHWARAM"
  },
  {
    "amount": 68558,
    "billdate": "2018-08-08",
    "outlet": "KOLAR"
  }
]

let formatData = function (data) {

             let billdates = [];
             let outlets = [];
             data.forEach(element => {
                 if (billdates.indexOf(element.billdate) == -1) {
                     billdates.push(element.billdate);
                 }
                 if (outlets.indexOf(element.outlet) == -1) {
                     outlets.push(element.outlet);
                 }
             });
             return {
                 data: data,
                 billdates: billdates,
                 outlets: outlets,
                 
             };
         };

  

         let renderTable = function (data) {
             billdates = data.billdates;
             outlets = data.outlets;
             data = data.data;
             let tbl = document.getElementById("tbl");
             let table = document.createElement("table");
             let thead = document.createElement("thead");
             let headerRow = document.createElement("tr");
             let th = document.createElement("th");
             th.innerHTML = "Bill___Date";
                th.classList.add("text-center");
             headerRow.appendChild(th);
             let grandTotal = 0;
             let outletWiseTotal = {};
             th = document.createElement("th");
             th.innerHTML = "Total";
               th.classList.add("text-center");
             headerRow.appendChild(th);
             outlets.forEach(element => {
                 th = document.createElement("th");
                 th.innerHTML = element;
                   th.classList.add("text-center");
                 headerRow.appendChild(th);
                 outletWiseTotal[element] = 0;
                 data.forEach(el => {
                     if (el.outlet == element) {
                         outletWiseTotal[element] += parseInt(el.amount);
                     }
                 });
                 grandTotal += outletWiseTotal[element];
             });
             
             thead.appendChild(headerRow);
             headerRow = document.createElement("tr");
             th = document.createElement("th");
             th.innerHTML = "Total";
              th.classList.add("text-center");
               
             headerRow.appendChild(th);

             outlets.forEach(element => {
                 th = document.createElement("th");
                 th.innerHTML = outletWiseTotal[element];
                    th.classList.add("text-right");   //ol totals
                 headerRow.appendChild(th);
             });
             th = document.createElement("th");
             th.innerHTML = grandTotal;
                 th.classList.add("text-right");  // grand total
         /*  console.log(grandTotal);*/
            // headerRow.appendChild(th);
           headerRow.insertBefore(th , headerRow.children[1] );
             thead.appendChild(headerRow);
             table.appendChild(thead);

             let tbody = document.createElement("tbody");

             
             billdates.forEach(element => {
              let row = document.createElement("tr");  
                 td = document.createElement("td");
                 td.innerHTML = element;
                 row.appendChild(td);
                 let total = 0;
                 outlets.forEach(outlet => {
                     let el = 0;
                     data.forEach(d => {
                         if (d.billdate == element && d.outlet == outlet) {
                             total += parseInt(d.amount);
                             el = d.amount;
                         }
                     });
                     td = document.createElement("td");
                     td.innerHTML = el;
                        td.classList.add("text-right"); //oldata
                     row.appendChild(td);
                 });
     /*console.log("row is : " , row.children )*/
                 td = document.createElement("td");
                 td.innerHTML = total;
                    td.classList.add("text-right"); //column total
                // row.appendChild(td);
     row.insertBefore(td , row.children[1] );
                 tbody.appendChild(row);
             });

             table.appendChild(tbody);

             tbl.innerHTML = "";
             tbl.appendChild(table);
             table.classList.add("table");
             table.classList.add("table-striped");
             table.classList.add("table-bordered");
         }
            let formatedData = formatData(data);
            renderTable(formatedData);
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet"
 href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
</head>
<body>
  <div id="tbl"></div>
</body>
</html>
halfer
  • 19,824
  • 17
  • 99
  • 186
manish thakur
  • 760
  • 9
  • 35
  • 76

2 Answers2

1

You can use this function to format your number:

let numberFormat = (str) => str.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

ES5:

function numberFormat(str){
    return str.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

Working example:

let num = 54321678;
let num2 = 123456;
let numberFormat = (num) => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(numberFormat(num));
console.log(numberFormat(num2));
halfer
  • 19,824
  • 17
  • 99
  • 186
Zainul Abideen
  • 1,829
  • 15
  • 37
0

You can use number.toLocaleString() to convert your integer to a comma-separated integer (as a string data type). You can then use .map to change your array's objects to have the property of amount set to their comma-separated value (as a string).

Take a look at the snippet below for a working example:

let data = [{"amount":291589,"billdate":"2018-08-01","outlet":"JAYANAGAR"},{"amount":58337,"billdate":"2018-08-01","outlet":"MALLESHWARAM"},{"amount":65970,"billdate":"2018-08-01","outlet":"KOLAR"},{"amount":296125,"billdate":"2018-08-02","outlet":"JAYANAGAR"},{"amount":56545,"billdate":"2018-08-02","outlet":"MALLESHWARAM"},{"amount":72213,"billdate":"2018-08-02","outlet":"KOLAR"},{"amount":346605,"billdate":"2018-08-03","outlet":"JAYANAGAR"},{"amount":62459,"billdate":"2018-08-03","outlet":"MALLESHWARAM"},{"amount":65248,"billdate":"2018-08-03","outlet":"KOLAR"},{"amount":518212,"billdate":"2018-08-04","outlet":"JAYANAGAR"},{"amount":104801,"billdate":"2018-08-04","outlet":"MALLESHWARAM"},{"amount":138151,"billdate":"2018-08-04","outlet":"KOLAR"},{"amount":628358,"billdate":"2018-08-05","outlet":"JAYANAGAR"},{"amount":115223,"billdate":"2018-08-05","outlet":"MALLESHWARAM"},{"amount":134107,"billdate":"2018-08-05","outlet":"KOLAR"},{"amount":177866,"billdate":"2018-08-06","outlet":"JAYANAGAR"},{"amount":66095,"billdate":"2018-08-06","outlet":"KOLAR"},{"amount":284069,"billdate":"2018-08-07","outlet":"JAYANAGAR"},{"amount":58789,"billdate":"2018-08-07","outlet":"MALLESHWARAM"},{"amount":67886,"billdate":"2018-08-07","outlet":"KOLAR"},{"amount":313128,"billdate":"2018-08-08","outlet":"JAYANAGAR"},{"amount":59939,"billdate":"2018-08-08","outlet":"MALLESHWARAM"},{"amount":68558,"billdate":"2018-08-08","outlet":"KOLAR"}];

const res = data.map(({amount, ...rem}) => ({amount: amount.toLocaleString(), ...rem}));
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64