在 C# 中将 Int 转换为浮点数
本指南将教我们在 C# 中将 int
转换为 float
。它可以简单地通过铸造来完成。
我们可以使用类型转换方法轻松地将 int
转换为 float
或 decimal
。让我们来看看。
在 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
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。