Search code examples
javascriptjsongridnumbersformat

thousand separated numbers in JSON Grid


In a JSON Grid , one column is defined as :-

{
"dataField": "Number of Days",
"caption":  "Number of Days",
"dataType": "number",
}

Output is :


Number of days 
4125490

What I expect is : Numbers should be displayed in thousand separated values with commas

4,125,497

Please help!


Solution

  • You can create a function for thousand separator using regex

    function thousandSeparator(num) {
      return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
    }
    
    console.log(thousandSeparator(4125490))

    Alternatively use toLocaleString to add comma.Both of this ways will return the result in string format

    console.log((4125490).toLocaleString('en'))