简体   繁体   中英

High Charts donut chart percentages adding to 100.1%

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 double s. 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

在此处输入图片说明

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM