Percentage calculation
Asked Answered
D

8

89

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

Diffidence answered 30/12, 2010 at 9:16 Comment(0)
D
98

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

Dead answered 30/12, 2010 at 9:26 Comment(9)
Thanks a lot..how to round that.. for ex ( 2 / 11) * 100 means what should i do...Diffidence
use Math.Round and then decide on how many decimal places you want :)Regulation
Cast it to an int. (int)((2.0 / 11.0) * 100.0) = 18.Dead
var completed = Math.Round(( 2 / 11), 2) * 100;Fideliafidelio
i had given like this...But it showing error... int DDLValues; int Count; int Result; Result = Math.Round((Count / DDLValues), 2) * 100;Diffidence
The error is "The call is ambiguous between the following methods or properties : 'System.Math.Round(decimal,int) and System.Math.Round(double,int)'Diffidence
Count and DDLValues are integers, so your division is an "integer division", returning and integer result (witch you dont want). Try casting Count or DDLValues to a double. (((double)Count / DDLValues)...Dead
Result = Math.Round((double)(Count / DDLValues), 2) * 100;...This also shows error."cannot convert type double to int"Diffidence
Consider the use of precision : double result = ((double)currentValue / (double)total) * 100;Absent
S
108

Using Math.Round():

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

or manually rounding:

int percentComplete = (int)(0.5f + ((100f * complete) / total));
Sandpaper answered 19/2, 2014 at 16:4 Comment(5)
So, complete and total should be int or double? Thanks.Planometer
It doesn't matter actually if you use it exactly as written here. Proving this statement is an exercise left to the reader :)Sandpaper
you should never use a floating point number (ie double) for moneyPastorship
If you are doing anything with money I would really, really hope you weren't googling 'how to calculate percentage' :P If you are, please do more reading on floating point precision, banker's rounding, and especially 'professional liability insurance'Sandpaper
@Sandpaper I am actually working with money and transactions and came here. I use decimal values myself, but wanted to be sure the percentage calculation would be correct.Phonic
D
98

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

Dead answered 30/12, 2010 at 9:26 Comment(9)
Thanks a lot..how to round that.. for ex ( 2 / 11) * 100 means what should i do...Diffidence
use Math.Round and then decide on how many decimal places you want :)Regulation
Cast it to an int. (int)((2.0 / 11.0) * 100.0) = 18.Dead
var completed = Math.Round(( 2 / 11), 2) * 100;Fideliafidelio
i had given like this...But it showing error... int DDLValues; int Count; int Result; Result = Math.Round((Count / DDLValues), 2) * 100;Diffidence
The error is "The call is ambiguous between the following methods or properties : 'System.Math.Round(decimal,int) and System.Math.Round(double,int)'Diffidence
Count and DDLValues are integers, so your division is an "integer division", returning and integer result (witch you dont want). Try casting Count or DDLValues to a double. (((double)Count / DDLValues)...Dead
Result = Math.Round((double)(Count / DDLValues), 2) * 100;...This also shows error."cannot convert type double to int"Diffidence
Consider the use of precision : double result = ((double)currentValue / (double)total) * 100;Absent
T
52

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%

Tsingyuan answered 8/4, 2015 at 7:12 Comment(0)
B
17

Mathematically, to get percentage from two numbers:

percentage = (yourNumber / totalNumber) * 100;

And also, to calculate from a percentage :

number = (percentage / 100) * totalNumber;
Babs answered 23/1, 2016 at 11:10 Comment(0)
V
15

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}";
Venavenable answered 19/1, 2018 at 16:25 Comment(1)
I had trouble with literally every other method presented here, but this one worked.Specify
E
6

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
Extramarital answered 22/7, 2021 at 7:19 Comment(0)
C
0

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);
Cockneyfy answered 31/5, 2021 at 5:43 Comment(0)
D
0

//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) + "%");
Discursion answered 9/1, 2023 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.