Let's say that we have a set of decimal values for which we would like to calculate the percentage of the total for each of those values:
decimal [] values = {20.50, 40.25, 70.10, 35.80}; // etc
We also have a total sum of those values:
decimal sum = 166.65;
Now, I would like to have for each of values
percentage representation, BUT so that in total, the sum of percents (int) is always equal to 100 as it should be.
Right now I use such formula for calculation of percentage value:
int percent = (int)(Math.Round(values[0] * 100.0m / sum));
but, depending on the input values - because of the rounding, the total SUM of calculated percentages for all of input values is sometimes 99 or 101.
What kind of formula to use to get the most accurate percentage distribution results?