Hello,

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 :)

Hello,
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"));

If you're still getting this error, can you post the error message?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.