为什么我不能调用Math.Round(double,int)重载 [英] Why I can't call Math.Round(double,int) overload
问题描述
在.NET库中有函数,如
System.Math.Round(double,int)
但是为什么我需要抛出double值来浮动才能使它工作??
看下面的截图:
以下函数
Math.Round(double value,int digits)
返回一个 double
。我看到你试图将一个名为 d
的 float
定义为数学的输出.Round(n,2)
其中 n
是值的两倍, 1.12345
和 2
表示使用以下代码的整数
double n = 1.12345;
float d = Math.Round(n,2);
你实际上会收到错误,因为上述函数的输出是 double
而不是 float
。
无法将double类型转换为float 。存在一个明确的转换(你错过了一个演员?)
您可以通过更改 float d = Math.Round(n,2);
to double d = Math.Round(n,2);
p>
谢谢,
希望你觉得有帮助:)
In .NET Library there is Function like
System.Math.Round(double, int)
But why I need to cast double value to float to make it work..??
Look on the following screenshot:
The following function
Math.Round(double value, int digits)
Returns a double
. I see that you have tried to define a float
of name d
to the output from Math.Round(n,2)
where n
is a double of value 1.12345
and 2
represents an integer using the following code
double n = 1.12345;
float d = Math.Round(n,2);
You'll actually get an error because the output from the above function is double
and not a float
.
Cannot implictly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)
You may fix this by changing float d = Math.Round(n,2);
to double d = Math.Round(n,2);
Thanks,
I hope you find this helpful :)
这篇关于为什么我不能调用Math.Round(double,int)重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!