I have a source value that I need to apply multiple percentages, but at the same time, and the percentages have to take into account the other percentages.
Take for instance:
Value = 100
Percentage1 = 10%
Percentage2 = 15%
Percentage3 = 17%
I need to work out what the final value would be, but when adding Percentage1 it has to take into account the values from Percentage2 and Percentage3.
The only way I have managed this at the moment is to recursively calculate the value with the last values of the other percentages until there are no more changes, but I'm not even sure if that is right.
Is there a smarter way of calculating this?
EDIT: This is basically to calculate multiple fees. So say you put up a listing on eBay and you get charged 10% for listing on eBay, and the buyer has bought through paypal, and you get charged 15% for a paypal transaction, and then you get charged a further 17% due to shipping, we are trying to work out what the final fees will be and then scale the value accordingly.
This is the code that I'm using:
Dim Value As Decimal = 100
Dim Fee1Percent As Decimal = 0.1
Dim Fee2Percent As Decimal = 0.15
Dim Fee3Percent As Decimal = 0.17
Dim PreviousFee1 As Decimal
Dim PreviousFee2 As Decimal
Dim PreviousFee3 As Decimal
Dim Fee1 As Decimal
Dim Fee2 As Decimal
Dim Fee3 As Decimal
Do
PreviousFee1 = Fee1
PreviousFee2 = Fee2
PreviousFee3 = Fee3
Fee1 = Math.Round((Value + PreviousFee2 + PreviousFee3) * Fee1Percent, 4, MidpointRounding.AwayFromZero)
Fee2 = Math.Round((Value + PreviousFee1 + PreviousFee3) * Fee2Percent, 4, MidpointRounding.AwayFromZero)
Fee3 = Math.Round((Value + PreviousFee1 + PreviousFee2) * Fee3Percent, 4, MidpointRounding.AwayFromZero)
Loop Until PreviousFee1 = Fee1 AndAlso PreviousFee2 = Fee2 AndAlso PreviousFee3 = Fee3
This is my recursive list, so you start with the initial value of 100, and then use the previous values of the other fees until there are no more changes (rounded to 4 decimal places)
10.0 15.00 17.00
13.200 19.0500 21.2500
14.0300 20.1675 22.4825
14.2650 20.4769 22.8136
14.3291 20.5618 22.9061
14.3468 20.5853 22.9315
14.3517 20.5917 22.9385
14.3530 20.5935 22.9404
14.3534 20.5940 22.9409
14.3535 20.5941 22.9411
14.3535 20.5942 22.9411
14.3535 20.5942 22.9411
So in this case my final sum is 157.8888
The issue was a wrong approach to my calculations: I had to mark up the base price using a reverse calc of the percentages
So using the 10%, 15% and 17%, here is the math: 100 / (1 - 0.1 - 0.15 - 0.17) = 172.414
And the proof:
10% - 17.2414
15% - 25.8621
17% - 29.3103
With a final total of 72.414, leaving, exactly 100.