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
8 Answers
Using Math.Round()
:
int percentComplete = (int)Math.Round((double)(100 * complete) / total);
or manually rounding:
int percentComplete = (int)(0.5f + ((100f * complete) / total));
-
So, `complete` and `total` should be `int` or `double`? Thanks. – Si8 Jul 14 '16 at 14:40
-
It doesn't matter actually if you use it exactly as written here. Proving this statement is an exercise left to the reader :) – Sogger Jul 15 '16 at 18:15
-
1you should never use a floating point number (ie double) for money – Stuart Dobson Sep 26 '17 at 09:53
-
9If 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' – Sogger Feb 24 '18 at 20:00
-
@Sogger 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. – I try so hard but I cry harder Nov 16 '22 at 09:03
(current / maximum) * 100
. In your case, (2 / 10) * 100
.

- 9,097
- 2
- 31
- 53
-
Thanks a lot..how to round that.. for ex ( 2 / 11) * 100 means what should i do... – RobinHood Dec 30 '10 at 09:30
-
1use Math.Round and then decide on how many decimal places you want :) – WestDiscGolf Dec 30 '10 at 09:31
-
1
-
-
i had given like this...But it showing error... int DDLValues; int Count; int Result; Result = Math.Round((Count / DDLValues), 2) * 100; – RobinHood Dec 30 '10 at 10:15
-
The error is "The call is ambiguous between the following methods or properties : 'System.Math.Round(decimal,int) and System.Math.Round(double,int)' – RobinHood Dec 30 '10 at 10:17
-
2Count 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)...` – Nicolas Repiquet Dec 30 '10 at 10:20
-
1Result = Math.Round((double)(Count / DDLValues), 2) * 100;...This also shows error."cannot convert type double to int" – RobinHood Dec 30 '10 at 10:25
-
Consider the use of precision : double result = ((double)currentValue / (double)total) * 100; – Reinhard Behrens Apr 28 '21 at 12:16
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%

- 7,601
- 1
- 30
- 33
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}";

- 14,009
- 6
- 49
- 77
-
2I had trouble with literally every other method presented here, but this one worked. – baker.nole May 16 '19 at 03:17
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

- 6,770
- 5
- 51
- 103
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);

- 1,484
- 1
- 19
- 33

- 11
- 3
//For other people
If you need calculate using negative numbers:
double minValue = -100;
double value = 30;
double maxValue = 100;
dobule perc = (value - minValue) / (maxValue - minValue);
Console.WriteLine((perc * 100) + "%");

- 136
- 1
- 8