C#以下方法或属性之间的调用不明确:'System.Math.Round(double, int)'和'System.Math.Round(decimal, int)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/771825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# The call is ambiguous between the following methods or properties: 'System.Math.Round(double, int)' and 'System.Math.Round(decimal, int)
提问by
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)
以下方法或属性之间的调用不明确:“System.Math.Round(double, int)”和“System.Math.Round(decimal, int)”
My code is
我的代码是
Math.Round(new FileInfo(strFilePath).Length / 1024, 1)
How can I fix this?
我怎样才能解决这个问题?
Thanks
谢谢
采纳答案by Lucero
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.
问题是您进行了整数除法(也会产生 an int
),并且 aint
可以隐式转换为double
and decimal
。因此,您需要确保表达式产生其中之一;double
可能是你想要的。
Math.Round(new FileInfo(strFilePath).Length / 1024.0, 1)
回答by Arcturus
Math.Round(new FileInfo(strFilePath).Length / 1024d, 1)
回答by edosoft
Math.Round((double) (new FileInfo(strFilePath).Length / 1024), 1)