什么是创建在C#中两个整数的百分比值的最佳方式是什么? [英] What's the best way to create a percentage value from two integers in C#?

查看:141
本文介绍了什么是创建在C#中两个整数的百分比值的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这篇关于什么是创建在C#中两个整数的百分比值的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆