我的Web应用程序遇到逻辑问题。
背景:整个页面上连续会有一堆div。它们的CSS width:
百分比等于项目的int
值除以列表的总int
值的百分比。
问题:我不希望任何宽度小于1%。这意味着我必须将小于1%到1%的项目的宽度增加,并减小所有其他项目的宽度,以便使百分比总计精确地增加到100%。
这就产生了一个问题,即原本就是1%的商品会减少到小于1%。
我已经尝试了很多方法来执行此操作,但这是我当前的迭代。通过将其放入控制台应用程序,我为自己和你们简化了工作。
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<Item>();
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 => 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 => x.Value) + " - " + items.Sum(x => x.Percent));
System.Console.WriteLine("\n");
var itemsLessThanOnePercent = items.Where(x => x.Percent < minPercentage).ToList();
var itemsGreaterThanOrEqualToOnePercent = items.Where(x => x.Percent >= minPercentage).ToList();
double totalPercentageLessThanOne = itemsLessThanOnePercent.Sum(x => 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 => x.Value) + " - " + items.Sum(x => x.Percent));
System.Console.ReadLine();
}
}
}
如您所见,这种情况会将
item
和Value = 8
减小到小于1%(0.009),因为item.Value
首先已经是1%。基本上,我希望
item
的百分比相对于item.Value
的总和,而没有任何小于1%的百分比,而总百分比等于100%。任何帮助,将不胜感激。
最佳答案
您可以按Value
递增的顺序枚举项目。对于每个项目,请分配百分比,如果小于最小值,则将溢出数继续分配给其余项目。好吧,我很难用语言来解释,所以这里是示例代码:
double overflow = 0;
int left = items.Count;
// loop in order of ascending value
foreach (var item in items.OrderBy(c => c.Value))
{
// calculate part of overflow to assign to this item, based on total and how much items still left
var overflowForItem = overflow / left;
// assign a bit less than expected if there is overflow
item.Percent = (item.Value - overflowForItem) / (double)sum;
// reduce overflow (we just assigned part of it above)
overflow -= overflowForItem;
if (item.Percent < minPercentage) {
overflow += (minPercentage - item.Percent) * sum;
item.Percent = minPercentage;
}
left--;
}
关于c# - 设置列表中小于1%到1%的数字,同时保持总数100%,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47699558/