Rounding to 100% for pie chart

笑着哭i 提交于 2020-04-19 18:58:15

问题


I am taking the sum 3 random existing whole numbers and then dividing each individual number by the sum to get the percentage.

What is the best way to guarantee I will always get 100%? Is there any vb.net function that will help me with this or do I basically have to manually find the difference between the sum of my 3 percentages and 100%, divide it, and add it to my 3 percentages.


回答1:


You calculate the percentages, add all but the last together and subtract from 100. If you ry to distribute the error, it won't help since it would get rounded off like in the first calculation.

Sometimes it might be best to add the error to the largest number so it won't be that much off




回答2:


How about rounding the last pie slice by subtracting the sum of the other slices from 100?

Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim percent1 As Decimal = 33.33333D
        Dim percent2 As Decimal = 12D
        Dim percent3 As Decimal = 100 - (percent1 + percent2)
        Dim totalPercent As Decimal = percent1 + percent2 + percent3
        MsgBox(totalPercent.ToString)
    End Sub
End Class



回答3:


This is a very common problem in statistical reporting and there is no real good way of handling it. To put it very blunt, the only way you can guarantee that rounded parts of a whole add up to the original whole every time, is to falsify your data. I know, this point of view is a little extreme, but nevertheless true. If you want to be true to your data, you should add up the rounded values and if they don’t add up to 100, display a footnote somewhere on your screen saying something like ”Percentages may not add up to 100 due to rounding”. The only way to represent the distribution correctly is by rounding them in the correct way.



来源:https://stackoverflow.com/questions/29823106/rounding-to-100-for-pie-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!