C#转换变量double浮动

时间:2018-10-27 18:49:24

标签: c# casting double

如何将包含双精度数的变量转换为浮点数?

我知道我们可以做到。

float deltaXPow = Math.Pow(2f, 3f);

但是我需要做的是强制转换变量。我已经尝试过以这种方式进行操作,但似乎无法正常工作。

float deltaYPow = Math.Pow((float)deltaY, (float)power2);

这不起作用。

编辑:

我正在做一个作业,以找到a点和b点的hyp和角度。这是用于Unity C#

float deltaX = firstValueX - secondValueX;
float deltaY = firstValueY - secondValueY;

//<Notes>
//These lines of code will find delta x and y squared.
//Created const double for 2 because this number does not change.
//<Notes>
const float power2 = 2;

float deltaXPow = Math.Pow(deltaX, power2);
Console.WriteLine($"Delta X to the power of 2 is {deltaXPow}.");

float deltaYPow = Math.Pow(deltaY, power2);
Console.WriteLine($"Delta Y to the power of 2 is {deltaYPow}.");

//<Notes>
//The following lines of code will add the two squared numbers together and 
//resolve to square root of that number.
//<Notes>
float hypotenuse = Math.Sqrt((float)deltaXPow + (float)deltaYPow);
Console.WriteLine($"The hypotenuse of the points entered is {hypotenuse}.");

//<Notes>
//
//<Notes>
float atanRad = Math.Atan2(deltaY, deltaX);

此代码出现以下错误=无法将'double'显式转换为'float'

因为这是C#统一,所以所有值都必须为浮点数。

1 个答案:

答案 0 :(得分:0)

对这个模糊的帖子表示抱歉。我已经用更多信息更新了帖子。

这已由马克解决。

(float)Math.Pow(variableX, variableY);

搞定了。