Ambiguous call to method “Round(a: System.Double): System.Double ; Round(d: System.Decimal): System.Decimal”

StackOverflow https://stackoverflow.com/questions/11523618

سؤال

I am bit confused as to why calling Math.Round method would raise, "Ambiguous Call" compiler error.

Here is my offending code:

Math.Round((2000-((Splots[x].RawMin/4095)*2000))+200);

RawMin is Int32 data type.

I thought, Round method should return with Int32 value type.

Any hints or clues will be greatly appreciated. Thanks,

هل كانت مفيدة؟

المحلول

Look at the overload list for Math.Round. There are two methods taking a single parameter:

double Round(double d)
decimal Round(decimal d)

Both are applicable when called with an int argument - but it's pointless to call the method when you've only got an int to start with, as it's already rounded.

I suspect you actually want to change how you're doing arithmetic, e.g. by performing the division operation in double arithmetic, which will then propagate to the other operations:

// Note the 4095.0 to make it a double
Math.Round((2000-((Splots[x].RawMin/4095.0)*2000))+200);

Without that, all the operations use integer arithmetic, which almost certainly wasn't what you wanted.

You'll still need to cast the result to int though. The range of double exceeds that of both int and long, which is why the return type of Math.Round(double) is double. In this case you "know" that the result will be in an appropriate range though, given the calculation.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top