HOWTO · Csharp

在 C# 中將 Int 轉換為浮點數

本指南將教我們在 C# 中將 int 轉換為 float。它可以簡單地通過型別轉換來完成。

本指南將教我們在 C# 中將 int 轉換為 float。它可以簡單地通過鑄造來完成。

我們可以使用型別轉換方法輕鬆地將 int 轉換為 floatdecimal。讓我們來看看。

C# 中將 Int 轉換為 Float

我們可以使用型別轉換將 int 轉換為 float。通過在 int 變數後面寫 (float)

例如,如果你的 int 變數是 temp_int,要將裡面的值轉換為 float 值,你需要做的就是編寫 (float)temp_int。看看下面的程式碼。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Int_to_Float {
  class Program {
    static void Main(string[] args) {
      // using type cast we can convert INT TO FLOAT OR DECIMAL...........
      int temp_int = 1;
      float temp_float = (float)temp_int;
      decimal temp_decimal = 3;
      float temp_float_decimal = (float)temp_decimal;
      Console.WriteLine("INT TO FLOAT= " + temp_float);
      Console.WriteLine("FLOAT TO DECIMAL= " + temp_float_decimal);

      Console.ReadKey();
    }
  }
}

程式碼的輸出如下所示。

output:
INT TO FLOAT= 1
FLOAT TO DECIMAL= 3