如何计算数字列表的百分比?

火焰

这是我要计算的示例。

我在列表中有4个不同的项目,每个项目有多少个。例如:

Box1 contains 3.2 items
Box2 contains 6.1 items
Box3 contains 4.0 items
Box4 contains 1.8 items

我想计算的是每个Box的百分比。结果如下所示:

Box1 contains 3.2 items = 17%
Box2 contains 6.1 items = 51%
Box3 contains 4.0 items = 24%
Box4 contains 1.8 items = 8%

Total = 100%
泽钦

由于这个问题是在没有语言标签的情况下提出的,因此我起草了Java解决方案。

计算项目总数:

double total= 0;
for(int i = 0; i < boxList.size(); i++)
{
  total += boxList[i].getItems();
}

计算每个框所占项目的百分比:

for(int i = 0; i < boxList.size(); i++)
{
  double ithPercentage = (boxList[i].getItems()/total) * 100.0d;
  //output the result
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章