0

I know this is basic one I'm asking, bear with me.

I'm trying to find out the percentage.

This is what I'm trying, but it is giving me an error of ambiguous call.

int total = 1;
int totalUsers = 3;
if (totalUsers > 0)
{
    var per =Math.Round(total / totalUsers,4) * 100 ;
    object[] args = new object[] { total, totalUsers, per };
    lblMsg.Text = string.Format("{0} of {1} users already voted({2}%)", args);
}

 

For Example:
if total = 1
totalusers = 3 
per should be after rounding of 33.33%
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
ankur
  • 4,565
  • 14
  • 64
  • 100
  • What are the compile-time types of `total` and `totalUsers`? And why are you creating an object with the results? What is the *exact* error message? (Don't answer these questions in a comment - edit your question.) – Jon Skeet Nov 02 '12 at 07:07
  • where are you getting `ambiguous call` error? – techBeginner Nov 02 '12 at 07:22

1 Answers1

1

To resolve the ambiguous call error, change to:

var per =Math.Round((decimal)total / totalUsers,4) * 100 ;
L.B
  • 114,136
  • 19
  • 178
  • 224
  • As per my above example per is 33.33 but when i pass this to string.format it is making per as 33.3300% , Any suggestions. – ankur Nov 02 '12 at 07:16
  • 1
    you can control that part in the string format like using `{2:N2}` – V4Vendetta Nov 02 '12 at 07:20
  • it is giving error "Input string was not in a correct format" when i made this change. – ankur Nov 02 '12 at 07:23
  • @dotNETbeginner it won't work as you would get only `33` (it evaluates to 0.33 when you say 2 decimal places) – V4Vendetta Nov 02 '12 at 07:23
  • @L.B is there any work around to avoid this extra zeros that are appended in string.format. – ankur Nov 02 '12 at 07:28
  • @ankur see the V4Vendetta's comment. use `"{0} of {1} users already voted({2:N2}%)"` – L.B Nov 02 '12 at 07:29
  • @L.B see my below comment of V4Vendetta's it is giving an error. – ankur Nov 02 '12 at 07:34
  • I have corrected that one if the round off is giving an whole number lets say 80 , it is adding 80.00 % can this be avoided. – ankur Nov 02 '12 at 07:38
  • @ankur your question changed from `ambiguous call` to `how to format a string`. Try this `"{0} of {1} users already voted({2:##.##}%)"` – L.B Nov 02 '12 at 07:46