我有两个整数,我要划分得到的百分比。
I have two integers that I want to divide to get a percentage.
这就是我现在所拥有的:
This is what I have right now:
int mappedItems = someList.Count(x => x.Value != null);
int totalItems = someList.Count();
(int)(((double)mappedItems /(double) totalItems) * 100)
这给出了正确的答案。但是,这是一个很大铸造做一些简单,只要得到两个数字之间的百分比。
This gives the right answer. But that is a lot of casting to do something as simple as get a percentage between two numbers.
有没有更好的方式来做到这一点?一些不涉及铸造?
Is there a better way to do this? Something that does not involve casting?
如何只 mappedItems * 100.0 / totalItems
和铸造为适当的类型?
How about just mappedItems * 100.0 / totalItems
and casting this to the appropriate type?