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

Check out below the simplest way to do this:

let number = 12345678;

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

Reference -> here

Related Questions

Sponsored Content

48 Answered Questions

76 Answered Questions

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

  • 2011-04-23 22:17:18
  • Walker
  • 5489957 View
  • 6813 Score
  • 76 Answer
  • Tags:   javascript arrays

65 Answered Questions

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

48 Answered Questions

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

26 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

31 Answered Questions

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

53 Answered Questions

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

37 Answered Questions

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

86 Answered Questions

[SOLVED] How do JavaScript closures work?

Sponsored Content