By Александр Ковтун


2018-06-13 14:11:13 8 Comments

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?

1 comments

@VicJordan 2018-06-13 14:14:07

Here is the simplest way to do this.

Check below:

let number = 12345678;

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

Reference -> here

Related Questions

Sponsored Content

88 Answered Questions

[SOLVED] How do JavaScript closures work?

60 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

52 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

34 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

64 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 4700962 View
  • 5920 Score
  • 64 Answer
  • Tags:   javascript arrays

48 Answered Questions

[SOLVED] Validate decimal numbers in JavaScript - IsNumeric()

24 Answered Questions

[SOLVED] For-each over an array in JavaScript?

26 Answered Questions

49 Answered Questions

Sponsored Content