[英]Getting Error When Converting from Decimal to Double in C#
I got an error and I don't know why. 我有一个错误,我不知道为什么。 I dont find the need to convert to double or am i supposed to to that?
我不认为需要转换为两倍,还是我应该这么做? I am really confused right now
我现在真的很困惑
Argument 1: cannot convert from 'decimal' to 'double' 参数1:无法从“十进制”转换为“双精度”
static void Main(string[] args)
{
Console.Write("speed: ");
string speed = Console.ReadLine();
Console.Write("Gammafaktor: ");
string Gammafaktor = Console.ReadLine();
{
}
var gamma1 = Convert.ToDecimal(Gammafaktor);
var speed1 = Convert.ToDecimal(speed);
if ( speed1 !=0 )
{
var calc = 1m / Convert.ToDecimal(Math.Sqrt(1 - speed1 * speed1));
Console.WriteLine(calc);
}
}
}
} }
You are most likely seeing: 您最有可能看到:
CS1503 Argument 1: cannot convert from 'decimal' to 'double'
CS1503参数1:无法从“十进制”转换为“双精度”
on the line with the Math.Sqrt
call, or (if you move the assignment out to a local): 在调用
Math.Sqrt
的行上,或者(如果将分配移到本地):
CS0266 Cannot implicitly convert type 'decimal' to 'double'.
CS0266无法将类型“十进制”隐式转换为“双精度”。 An explicit conversion exists (are you missing a cast?)
存在显式转换(您是否缺少演员表?)
Math.Sqrt
takes a double
, not a decimal
, and the conversion from decimal
to double
is explicit , not implicit - meaning it isn't going to just do it automatically without you knowing about it; Math.Sqrt
接受double
而不是decimal
,并且从decimal
到double
的转换是显式的 ,而不是隐式的 -这意味着它不会在不知情的情况下自动执行。 so: 所以:
var calc = 1m / Convert.ToDecimal(Math.Sqrt((double)(1 - speed1 * speed1)));
As a side note... that calculation looks very odd (and dangerous), unless speed1
is always between zero and one. 附带说明一下...除非
speed1
始终在零和一之间,否则该计算看起来非常奇怪(危险)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.