HI Pierre Smits
I think formula should be:
percProfit = averageCost != 0 ? (profit / averageCost) * 100.00 : 0.00; and
totalPercProfit = totalCost != 0 ? (totalProfit / totalCost) * 100.00: 0.00;
I am saying this based on discussion given here: http://math.stackexchange.com/questions/105306/what-is-the-formula-to-calculate-profit-percentage
So formula given in your patch gives profit margin percentage and formula I am giving will give profit mark up percentage.
Different people have different opinion on this formula. So we can conclude this and then push community to commit the right solution.