百分比计算

发布于 2024-10-09 13:40:44 字数 155 浏览 0 评论 0原文

我正在 ASP.NET MVC 2 中研究进度条概念。这里我有一个 DropDownList,它有 10 个值。我想计算进度条的百分比,例如 DropDownList 中的 10 个值,并且我有一个返回值 2 的查询。因此,在 10 个值中我得到 2。应该显示“20 % 已完成”。如何做这个计算

I am working in progress bar concept in ASP.NET MVC 2. Here i have a DropDownList which has 10 values. i want to calculate the percentage for progress bar, e.g. 10 values from DropDownList and i am having a query which returns the value 2. so, out of 10 values i am getting 2. "20 % completed" should be displayed.. How to do this calculation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

执手闯天涯 2024-10-16 13:40:44

使用 Math.Round()

int percentComplete = (int)Math.Round((double)(100 * complete) / total);

或手动舍入:

int percentComplete = (int)(0.5f + ((100f * complete) / total));

Using Math.Round():

int percentComplete = (int)Math.Round((double)(100 * complete) / total);

or manually rounding:

int percentComplete = (int)(0.5f + ((100f * complete) / total));
破晓 2024-10-16 13:40:44

(当前/最大值)* 100。在您的情况下,(2 / 10) * 100

(current / maximum) * 100. In your case, (2 / 10) * 100.

独享拥抱 2024-10-16 13:40:44

使用 C# 字符串格式,您可以避免乘以 100,因为它会使代码更短、更清晰,特别是因为括号更少,而且还可以避免向上舍入代码。

(current / maximum).ToString("0.00%");

// 输出 - 16.67%

With C# String formatting you can avoid the multiplication by 100 as it will make the code shorter and cleaner especially because of less brackets and also the rounding up code can be avoided.

(current / maximum).ToString("0.00%");

// Output - 16.67%

北凤男飞 2024-10-16 13:40:44

从数学上讲,从两个数字获取百分比:

percentage = (yourNumber / totalNumber) * 100;

并且从百分比计算:

number = (percentage / 100) * totalNumber;

Mathematically, to get percentage from two numbers:

percentage = (yourNumber / totalNumber) * 100;

And also, to calculate from a percentage :

number = (percentage / 100) * totalNumber;
雄赳赳气昂昂 2024-10-16 13:40:44

您可以将百分比保留为十进制(值\总计),然后当您想要渲染给人类时,您可以使用Habeeb 的答案或使用字符串插值 你可以有更干净的东西:

var displayPercentage = $"{(decimal)value / total:P}";

//Calculate percentage earlier in code
decimal percentage = (decimal)value / total;
...
//Now render percentage
var displayPercentage = $"{percentage:P}";

You can hold onto the percentage as decimal (value \ total) and then when you want to render to a human you can make use of Habeeb's answer or using string interpolation you could have something even cleaner:

var displayPercentage = $"{(decimal)value / total:P}";

or

//Calculate percentage earlier in code
decimal percentage = (decimal)value / total;
...
//Now render percentage
var displayPercentage = $"{percentage:P}";
千纸鹤 2024-10-16 13:40:44

请记住,如果您有两个整数,您可能需要将其中一个数字转换为双倍

(double)i / events.Count * 100

Bear in mind that you may need to cast one of the numbers to double if you have two ints

(double)i / events.Count * 100
折戟 2024-10-16 13:40:44

就我而言,我设置了两个整数,并尝试计算百分比,但总是得到 0;

我的代码(之前)

int Ff_Crm_Count = Ff_Crm.Count();
int Unfollowed_Ff_Crm_Count = Unfollowed_Ff_Crm.Count();
int The_Percentage = (Unfollowed_Ff_Crm_Count / Ff_Crm_Count) * 100);

在研究之后(之后)

double Ff_Crm_Count = Ff_Crm.Count();
double Unfollowed_Ff_Crm_Count = Unfollowed_Ff_Crm.Count();
double The_Percentage = Math.Round((double)((Unfollowed_Ff_Crm_Count / Ff_Crm_Count) * 100),2);

In my case, I set two ints, and trying to calculate the percentage, and always get 0;

my code (before)

int Ff_Crm_Count = Ff_Crm.Count();
int Unfollowed_Ff_Crm_Count = Unfollowed_Ff_Crm.Count();
int The_Percentage = (Unfollowed_Ff_Crm_Count / Ff_Crm_Count) * 100);

after doing research (after)

double Ff_Crm_Count = Ff_Crm.Count();
double Unfollowed_Ff_Crm_Count = Unfollowed_Ff_Crm.Count();
double The_Percentage = Math.Round((double)((Unfollowed_Ff_Crm_Count / Ff_Crm_Count) * 100),2);
梦回旧景 2024-10-16 13:40:44

//对于其他人

如果需要使用负数计算:

double minValue = -100;
double value = 30;
double maxValue = 100;
double perc = (value - minValue) / (maxValue - minValue);
Console.WriteLine((perc * 100) + "%");

//For other people

If you need calculate using negative numbers:

double minValue = -100;
double value = 30;
double maxValue = 100;
double perc = (value - minValue) / (maxValue - minValue);
Console.WriteLine((perc * 100) + "%");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文