请问C#的float如何转换为int

 时间:2024-10-14 14:06:06

int到float转换精度会降低,如果需要较高精度,应该考虑使用double类型。

剧安颌儿如从int(System.Int32)到float转换精度会降低,如下代码:

static void Main(string[] args)

{

Int32 number = Int32.MaxValue;

Console.WriteLine(number);

Console.WriteLine((float)number);

Console.ReadLine();

}

输出为:

2147483647

2.147484E+09

请问C#的float如何转换为int

扩展资料:

当在int(假设int是32位的)、float和double格式之间进行强制类型转换时,原则如下:

从 int 转换成 float,数字不会溢出,但是可能被舍入。

从 int、float 转换成 double,能够保留精确的数值。因为 double 有更大的范围和更高的精度(有效位数)。

从 double 转换成 float,因为 float 范围要小一些,所以值可能溢出成 +∞ 或 -∞。另外由于float精度较小,还可能被舍入。

从 float、double 转换成 int,值将会向零舍入。如1.999会被转成1,-1.999会被转成-1。同时值可能会溢出。

参考资料来源:百度百科-c#

热门搜索
后背酸痛是怎么回事 生板栗怎么去壳 怎么算生辰八字 克徕帝钻戒怎么样 妈妈英文怎么写 名字中间的点怎么打 辞职后五险一金怎么处理 shower怎么读 下巴长痘痘是什么原因怎么调理 溜溜球怎么玩