在 C# 中将 Int 转换为浮点数

本指南将教我们在 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