title: Setting number in list that's less than 1% to 1% while keeping total 100%
body: <p>I'm running into a logic issue with my web application.</p>
  
  <p>Background: There will be a bunch of div's in a row across the page. Their CSS <code>width:</code> percentage is equal to the percentage the item's <code>int</code> value divided by the list's total <code>int</code> value.</p>
  
  <p>The problem: I don't want any of the widths to be less than 1%.  This means I have to increase the width of items lower than 1% to 1%, and reduce the widths of all of the other items so the percentage total adds back up to 100% exactly.</p>
  
  <p>This creates an issue where an item that was already exactly 1% gets reduced to less than 1%.</p>
  
  <p>I've tried so many ways to do this, but this is my current iteration.  I made it simple for myself and you guys by putting it into a console application.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace PercentageOfTotal
  {
      public class Item
      {
          public int Value { get; set; }
          public double Percent { get; set; }
      }
  
      class Program
      {
          static void Main(string[] args)
          {
              var items = new List&lt;Item&gt;();
  
              items.Add(new Item { Value = 26 });
              items.Add(new Item { Value = 31 });
              items.Add(new Item { Value = 47 });
              items.Add(new Item { Value = 175 });
              items.Add(new Item { Value = 50 });
              items.Add(new Item { Value = 1 });
              items.Add(new Item { Value = 74 });
              items.Add(new Item { Value = 8 });
              items.Add(new Item { Value = 219 });
              items.Add(new Item { Value = 169 });
  
              int sum = items.Sum(x =&gt; x.Value);
              double minPercentage = 0.01;
  
              System.Console.WriteLine("Value - Percent");
  
              foreach (var item in items)
              {
                  item.Percent = item.Value / (double)sum;
                  System.Console.WriteLine(item.Value + " - " + item.Percent);
              }
  
              System.Console.WriteLine("---------");
              System.Console.WriteLine(items.Sum(x =&gt; x.Value) + " - " + items.Sum(x =&gt; x.Percent));
              System.Console.WriteLine("\n");
  
              var itemsLessThanOnePercent = items.Where(x =&gt; x.Percent &lt; minPercentage).ToList();
              var itemsGreaterThanOrEqualToOnePercent = items.Where(x =&gt; x.Percent &gt;= minPercentage).ToList();
              double totalPercentageLessThanOne = itemsLessThanOnePercent.Sum(x =&gt; x.Percent);
              double reduceEachGreaterThanOneBy = ((itemsLessThanOnePercent.Count() * minPercentage) - totalPercentageLessThanOne) / itemsGreaterThanOrEqualToOnePercent.Count();
  
              foreach (var item in itemsGreaterThanOrEqualToOnePercent)
              {
                  item.Percent = item.Percent - reduceEachGreaterThanOneBy;
              }
  
              foreach (var item in itemsLessThanOnePercent)
              {
                  item.Percent = minPercentage;
              }
  
              System.Console.WriteLine("Value - Percent");
  
              foreach (var item in items)
              {
                  System.Console.WriteLine(item.Value + " - " + item.Percent);
              }
  
              System.Console.WriteLine("---------");
              System.Console.WriteLine(items.Sum(x =&gt; x.Value) + " - " + items.Sum(x =&gt; x.Percent));
              System.Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>As you can see, this case it reduces the <code>item</code> with <code>Value = 8</code> to less than 1% (0.009) because the <code>item.Value</code> was already 1% in the first place.</p>
  
  <p>Basically, I want the <code>item</code>'s percentage to be relative to the overall sum of <code>item.Value</code> without having any percentages less than 1% and the total of percentages equaling 100%.</p>
  
  <p>Any help would be appreciated. </p>