0

Is that any elegant way to convert json response into comma separated number (for showing currency purpose).

This is what I have so far:

let data = {      
   "business":{        
      "trasactionTableData":[  
         {  
            "Date":"2019-08-19T00:00:00",
            "Item Details":"Plastic - Grade A",
            "slotShare":"30",
            "assetBeforeonMarket":"24.000000",
            "profitAfterSales":"0.000000",
            "actualProfit":"22",
            "actualAmount":"USD 10.00"
         },
         {  
            "Date":"2019-08-19T00:00:00",
            "Item Details":"Cashless for Cash",
            "slotShare":"32",
            "assetBeforeonMarket":"60.000000",
            "profitAfterSales":"60.000000",
            "actualProfit":"0",
            "actualAmount":"USD 7400.10"
         },
         {  
            "Date":"2019-08-19T00:00:00",
            "Item Details":"Cashless for Cash",
            "slotShare":"33",
            "assetBeforeonMarket":"81.000000",
            "profitAfterSales":"81.000000",
            "actualProfit":"0",
            "actualAmount":"USD 200.59"
         }
      ],      
   }
}


 for (let i = 0; i < data.trasactionTableData.length; i++) {
        let slotShare = this.eosTblData[i].slotShare;
        let actualAmount = this.eosTblData[i].actualAmount;
        /*
    // need to convert below as well
    let assetBeforeonMarket = this.eosTblData[i].assetBeforeonMarket;
        let profitAfterSales = this.eosTblData[i].profitAfterSales;*/

        console.log('before convert', slotShare, 'After convert', String(slotShare).replace(/(.)(?=(\d{3})+$)/g,'$1,'));
    }

but what I'm getting is before convert 24.000000 After convert 24.,000,000

Expected result for this example:

566,556,00

How to approach this in elegant way to achieve this conversion?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mr. Learner
  • 978
  • 2
  • 18
  • 48
  • Possible duplicate of [Javascript Thousand Separator / string format](https://stackoverflow.com/questions/3753483/javascript-thousand-separator-string-format) – devDan Sep 23 '19 at 10:48
  • 1
    Use the [currency pipe](https://angular.io/api/common/CurrencyPipe) : it doesn't change your data but displays it as you want –  Sep 23 '19 at 10:49
  • 1
    @dince12 this answer is suited for JS, Angular has another way of doing it –  Sep 23 '19 at 10:50

3 Answers3

2

You can use toLocalString to format your number.

e.g.:

const number = 5000;
const localeNumber =  number.toLocaleString('en-GB');
console.log(localeNumber); // log 5,000
Amadou Beye
  • 2,538
  • 3
  • 16
  • 37
Norbert Bartko
  • 2,468
  • 1
  • 17
  • 36
2

Yes, there is. If you just want to show the currency formatted, without changing its value, you can use the CurrencyPipe, like this:

    <p>{{ value | currency:'USD' }}</p>
dpolicastro
  • 1,379
  • 1
  • 14
  • 22
2

Simply use the number pipe instead.

To give an example:

{{ '1234567' | number:'.2'}}

DEMO

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396