javascript - Adding commas to large numbers in d3.js - Stack Overflow

I have data points on a line, when the cursor passes over them, a tooltip div appears with data about t

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 mas 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

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 mas 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

Share Improve this question asked Mar 13, 2013 at 14:06 DaftDaft 11k16 gold badges65 silver badges105 bronze badges 1
  • can u show some example? – iJade Commented Mar 13, 2013 at 14:27
Add a ment  | 

3 Answers 3

Reset to default 4

Solution: You can't add mas using d3 but you can do via plain JavaScript: Number.toLocaleString()

// convert Avengers to a Number and use the toLocaleString on it
div.html("The Avengers Box Office: $" + (1*d.Avengers).toLocaleString() ) 

Advanced Solution: If your main problem is that numbers are just too big (even with mas), you need write some really cool conversion functions for your numbers that

  1. count the decimals and
  2. choose a really nice format to display nice numbers

Check out a somehow related answer where I added some code to do that, converting big numbers to "kilo", "mega", "giga", and "terra". You might want to adopt that.

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

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

d3.format(',')(1999222) //// output=1,999,222

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637056a4637459.html

相关推荐

  • javascript - Adding commas to large numbers in d3.js - Stack Overflow

    I have data points on a line, when the cursor passes over them, a tooltip div appears with data about t

    22天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信