I'm writing a program that compares two percentages and I need them to be rounded up or down depending on their number.. I have used this:

percentage[i] = Math.Round ((analysis[i] * 100) / char_count);

Bit it throws an error that it's too ambiguous.. Now the "analysis" is a double type and char_count is an integer.

Anyone make any suggestions? Thanks :)

this example works without any errors:

double a = 1.2;
int c = 2;
double res = Math.Round((a * 100) / c);

Maybe you want somehow extend the problem definition ;)

To avoid unexpected results, it helps to cast all variables to a common datatype prior to performing calculations:

double percentage[] = new double[1];
percentage[0] = Math.Round (((double)analysis[i] * 100d) / (double)char_count);

Furthermore, it makes more sense to only perform rounding as the last step - such as displaying the output (or to conform to sigdigs I suppose):

double ratio = (double)analysis[i] / (double)char_count;
MessageBox.Show("Percentage:" + (ratio[0] * 100d).ToString("p"));

Edited 5 Years Ago by skatamatic: n/a

This article has been dead for over six months. Start a new discussion instead.