52

My code won't compile due to the error below:

The call is ambiguous between the following methods or properties: 'System.Math.Round(double, int)' and 'System.Math.Round(decimal, int)

My code is

Math.Round(new FileInfo(strFilePath).Length / 1024, 1)

How can I fix this?

Thanks

3 Answers3

54
Math.Round(new FileInfo(strFilePath).Length / 1024d, 1)
Arcturus
  • 26,677
  • 10
  • 92
  • 107
  • 6
    This is way better than the accepted answer, you should not implicitly cast using ".0", using 'd' suffix is explicit and preferred. – user275587 Jun 09 '10 at 08:54
  • 3
    Better yes, but the explanation in the accepted answer as to _why_ is extremely helpful. – jmgardn2 Oct 09 '15 at 18:57
41

The problem is that you make an integer division (results also in an int) and a int can be implicitly converted to both double and decimal. Therefore, you need to make sure the expression results in one of those; double is probably what you want.

Math.Round(new FileInfo(strFilePath).Length / 1024.0, 1)
BornToCode
  • 9,495
  • 9
  • 66
  • 83
Lucero
  • 59,176
  • 9
  • 122
  • 152
14
Math.Round((double) (new FileInfo(strFilePath).Length / 1024), 1)
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
edosoft
  • 17,121
  • 25
  • 77
  • 111
  • why is this a negative 1? Please explain yourself negative rater so we understand why you did this. Seems logical to me to just cast it like he did. – PositiveGuy Aug 18 '11 at 15:54
  • I disagree with down vote since the answer is correct if you oversee the easy-to-make mistake. But you did need to put the `new FileInfo(strFilePath).Length / 1024` in parenthesis to make it work. – Sellorio May 26 '13 at 23:08
  • 2
    The problem is that the divisions will still be integer division which will truncate, so rounding is pointless even if you cast to a double. – juharr Feb 08 '16 at 19:48