Charlie Charlie - 3 months ago 15
C# Question

Add a number in list so it becomes 20 percent

I want to add a number in a list of numbers so that after adding this number in that list, it is 20 percent of whole list.

The goal is to set width of first column absolute (20%) and relative for others columns while creating table in itextsharp PDF.

For example here is a list {10,30,20,20}. What should I add in it which is also 20% of whole sum of list, which is 20 ( 20+10+30+20+20 = 100, 20/100 -> 20%).

Answer

Some basic math like 0.20 = x / (Sum + x) give you x = Sum/4. So Enumerable.Sum will give you most of the result:

listOfInt.Add (listOfInt.Sum()/4);
Comments