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

默认分类 未结 1 647
0o笑笑宝贝o0
0o笑笑宝贝o0 2023-03-18 13:36
相关标签:
1条回答
  • 2023-03-18 13:48

    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

    扩展资料:

    当在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#

    没一个正确答案,真是不靠谱,其实很简单float count = 0.035f/0.01f;int countI =(int) count / 1;

    PointF p1 = new PointF(0, 0);PointF p2 = new PointF(X, Y);g.DrawLine(pe, X, Y);

    强制转换,a = (int)b;

    1.a=Integer.parseInt(b.toString());2.a=Convert.ToInt32(b);

    a=(int) b;

    0 讨论(0)
提交回复