Search code examples
javascriptd3.jsstring-formatting

Adding commas to large numbers in d3.js


I have data points on a line, when the cursor passes over them, a tooltip div appears with data about that point. The code for the html of the div is below.

    div.html("The Avengers Box Office: $" + d.Avengers) 

d.Avengers returns 207438708 as a string. So is there any way to add commas to large numbers in d3.js?

I have values ranging from thousands to hundreds of millions and it can be tricky to read them.

Thanks


Solution

  • Thanks for the replies guys. I found the answer here. Its not adding commas, but its adding M or K after millions or thousands. Which is actually better I think.

    https://groups.google.com/forum/?fromgroups=#!topic/d3-js/YFsSmzu4JZk