1

I have a number of unknown length and i need to format it by adding comma after every 3rd number from the right (so 12345678 becomes 12,345,678). As of now i'm using this chaos to do it

value.toString().split('').reverse().join('').match(/.{1,3}/g).reverse().map( (num) => num.split('').reverse().join('') ).join()

Is there any cleaner way to do it with native functionality?

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • 3
    Possible duplicate of [How to print a number with commas as thousands separators in JavaScript](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Eddie Jun 13 '18 at 14:12

1 Answers1

8

Check out below the simplest way to do this:

let number = 12345678;

console.log(number.toLocaleString("en"));

Reference -> here

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104