show json data in indian rupees format

73 views Asked by At
[
    
    {
        "id": "1",
        "accountName": "Account 1",
        "debitAmount": "15453435",
        "creditAmount":"313244"
        
    },
    {
        "id": "2",
        "accountName": "Account 2",
        "debitAmount": "15453435",
        "currencyCode": "INR",
        "creditAmount":"313244"
        
    }
]

This is my jason code i want to print the the numbers in indian rupee format like 12,43,434 not able to do that i have been trying searching bu did no got any solution.

2

There are 2 answers

0
Ali Sattarzadeh On

you can use regex to add comma in order to convert numbers

like this :

const numberWithCommas = (num) => {
    if (num) {
        const x = num.toString().split('.');
        const result = x ? x[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',') : x;
        if (x[1]) return `${result}.${x[1]}`;
        return result;
    }
    return num;
};




const data  = [
    
    {
        "id": "1",
        "accountName": "Account 1",
        "debitAmount": "15453435",
        "creditAmount":"313244"
        
    },
    {
        "id": "2",
        "accountName": "Account 2",
        "debitAmount": "15453435",
        "currencyCode": "INR",
        "creditAmount":"313244"
        
    }
]

const result = data.map(item => ({...item,debitAmount:numberWithCommas(item.debitAmount),creditAmount:numberWithCommas(item.creditAmount) }))

console.log(result)

0
Neetigya Chahar On

You can use a package indian-number-format for the conversion

const fmt = require('indian-number-format')
// format
console.log(fmt.format(270371))                 // prints: 2,70,371
console.log(fmt.format(123456789327.6452))      // prints: 12,345,67,89,327.6452