0

I created a donut chart using High Charts as shown below. My problem is that the subsets are totaling to 100.1%. I am not rounding and my numbers are all of type doubles. Does anyone have an idea as to where my math could go wrong to get this ever-so-slight problem?

7.7 + 7.0 + 15.3 + 70.1 = 100.1

Chart

enter image description here

Paweł Fus
  • 44,795
  • 3
  • 61
  • 77
Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
  • Could you please provide a bit more of "surrounding" information? Some more code? – sshashank124 Mar 19 '14 at 13:49
  • @sshashank124 What kind of information are you looking for? - I would be glad to include it. The amount of code I use to generate this chart is far too much to post. I am mainly looking for 'possible' problems or a push in the right direction. – Jordan.J.D Mar 19 '14 at 13:51
  • Where is the chart getting its values from? How are you creating it? What data types are you using for your numbers? – sshashank124 Mar 19 '14 at 13:51
  • @sshashank124 In my question I include data types are all `double`, The chart is getting its values from a JSON object I created using queries. The data is used once before and generates another donut chart correctly (which is why I am confused about this one). – Jordan.J.D Mar 19 '14 at 13:53
  • What are the data values you are using for this chart? – jing3142 Mar 19 '14 at 13:58
  • @JordanD. Ok so firstly, the sum is 100.1 not 101.1 but that's not important. Secondly, I doubt your High Charts module is messing up your data so it must be what you are passing in. I recommend checking what you are passing in by printing it to the html screen by document or something like that. – sshashank124 Mar 19 '14 at 14:00
  • @sshashank124 sorry for the silly algebra error, I fixed it. After a few console logs it seems doubles are rounding to 1 decimal places. – Jordan.J.D Mar 19 '14 at 14:28
  • @JordanD, nice! You should post your solution as an answer so that others in the future may refer to it. – sshashank124 Mar 19 '14 at 14:29
  • @sshashank124 After I find a way to truncate the double instead of rounding it I will post my answer. – Jordan.J.D Mar 19 '14 at 14:31
  • @JordanD, An easy way of truncating a double is to `round(num-1)`. This gives the same effect as truncation. The method I gave is just pseudocode not actual code – sshashank124 Mar 19 '14 at 14:32
  • Surely the values presented in those labels are rounded. If not, you have the most unusually precise data I've ever seen, to have only 1 decimal place for every single percentage... – jlbriggs Mar 20 '14 at 20:23

2 Answers2

4

It's just caused by rounding done in formatter. Let's consider this example: http://jsfiddle.net/MkuMS/

Sum is: 74.5+14.1+10.3+1.2 = 100.1

Now, let's remove rounding from dataLabels.format: http://jsfiddle.net/MkuMS/1/

Sum is: 74.50331125827813+14.072847682119205+10.264900662251655+1.1589403973509933 = 100

Now, it's up to you, or you are going to display with rounding error, or full info in labels (which looks.. bad) or create your own dataLabels.formatter.

But you will still encounter issue with non rounding values: http://jsfiddle.net/MkuMS/3/

Sum is: 33.3+33.3+33.3 = 99.9

Paweł Fus
  • 44,795
  • 3
  • 61
  • 77
0

It may be because of the double type. It's not precise enough in some cases. Try using BigDecimal instead.

More info: Double vs. BigDecimal?

Community
  • 1
  • 1
StaNov
  • 332
  • 1
  • 4
  • 17