
请问C#的float如何转换为int
现在需要a得到b的整数部分值 可是编译提示
无法将类型“float”隐式转换为“int”。存在一个显式转换(是否缺少强制转换?)
请问要怎样才可以让a成功获得b的整数部分数值? 展开
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;
a=Integer.parseInt(b.toString());
2.
a=Convert.ToInt32(b);
PointF p2 = new PointF(X, Y);
g.DrawLine(pe, X, Y);