簡體   English   中英

C# 將變量 double 轉換為浮點數

[英]C# casting variable double to float

如何將包含雙精度的變量轉換為浮點數?

我明白我們可以做到這一點。

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# unity,所以所有值都需要浮點數。

抱歉,帖子含糊不清。 我已經用更多信息更新了帖子。

馬克解決了這個問題。

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

做到了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM