C# 如何找到百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8894834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find percentage
提问by Jeff Hardy
I want to show risk ratio in my application.
我想在我的申请中显示风险比率。
I want to get this value in percent for example
例如,我想以百分比形式获得此值
let a = 100, b = 50;
让 a = 100, b = 50;
I want to show the value in percentage value = a - b; = 50;
我想以百分比值显示值 = a - b; = 50;
I want to show this in percentage.
我想以百分比显示这一点。
any suggestions.
有什么建议。
Thanks In Advance.
提前致谢。
采纳答案by Eugen Rieck
let percentage=100*(a-b)/a
and maybe you will want to round it
也许你会想要圆它
回答by kamui
If I understand your question properly,
如果我正确理解你的问题,
if a=total and b is the part
如果 a=total 并且 b 是部分
then b/a*100 = the percentage taken
那么 b/a*100 = 所占的百分比
Double a = 100;
Double b = 50;
Double percentage = (b/a*100);
// to output the result
Labelcontrol.Text = percentage.ToString();
// or if just a plain c# app you can send it to the console
Console.WriteLine(percentage.ToString());
Update: I have realised you may want the other way around where you want the percentage left once you remove the value. In that case the calculation line is simply updated to:
更新:我已经意识到,一旦删除该值,您可能希望以相反的方式保留百分比。在这种情况下,计算行简单地更新为:
Double percentage = 100*(a-b)/a;
回答by Fabio
Here's an example using string.format to format the result (C#, as you've tagged the question with)
这是一个使用 string.format 格式化结果的示例(C#,因为你已经用它标记了问题)
int a = 100, b = 50;
double p = (double)(a - b) / a;
string s = string.Format("Result is {0:0.0%}", p);

