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
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Using
Math.Round()
:or manually rounding:
Mathematically, to get percentage from two numbers:
And also, to calculate from a percentage :
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.
// Output - 16.67%
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:or
(current / maximum) * 100
. In your case,(2 / 10) * 100
.