## Percentage calculation with negative numbers

Suppose you have 2 numbers. You want to sum them up, and calculate the percentage of each number upon that sum. Say, 4 and 6. So 4 contributes 40% and 6 contributes 60% to their sum 10.

What happens when you have negative numbers?

I did some simple research, and the relevant articles have someone trying to calculate percentage changes from one number to another. Like financial growth reports.

My question is more of, how much each component number contributes to the sum, as a percentage. The problem comes when one of the numbers is negative. Consider the trivial case, 1 and -1. The sum is 0. You already get a division by zero error when calculating the percentage (1/0) * 100.

The solution, which is the same as that in my research, is to take the difference between the two numbers and use that as the basis. So difference of 1 and -1 is 2. So 1 contributes (1/2)*100 = 50%.

What about -1? Use the absolute function. ( abs(-1) / 2 ) * 100 = 50%.

The difference method works fine if you have only two numbers. What if you work with several? My friend actually posed this question to me. Suppose you have 6 numbers, and you want to calculate the percentage contribution for each number towards their sum.

My suggestion? Absolute everything. The percentage contribution of n1 is
abs(n1) / ( abs(n1) + abs(n2) + abs(n3) + abs(n4) + abs(n5) + abs(n6) ) * 100

Then my friend posed a killer question. What if all the numbers are zero? What’s the percentage for each number then (even though each number is zero)?

It’s for a reporting application, and my friend was wondering how to calculate the percentages. Now the sum of a bunch of zeroes is also zero. You hit the division by zero error. Even the absolute-everything method fails, since each number is zero, so there’s nothing to “absolute”.

Since there’s no defined way of calculating when all the numbers are zero, I gave the 2 obvious solutions. The first is that, since each number is zero, and the sum is zero, therefore each zero contributed 100% to the zero sum. The second is, since each number is zero, therefore each contributed 0%.

My friend chose the second solution. The most compelling reason for that choice was that it’s easier to explain the logic behind that choice to the user. It’s an edge case. When there’s no right answer, choose the answer which is easier to explain.

UPDATE: Steve has given 2 more alternative solutions.

UPDATE: I wrote an article to explain some of the confusion by some readers. How can a poorly performing product contribute the highest percentage in a company’s bottom line? Read the explanation here.

If you enjoyed this article and found it useful, please share it with your friends. You should also subscribe to get the latest articles (it’s free).