什么是创建在C#中两个整数的百分比值的最佳方式是什么?比值、整数、百分、两个

2023-09-04 22:40:27 作者:Memory -痴情゛

我有两个整数,我要划分得到的百分比。

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?

 
精彩推荐
图片推荐