[英]How do I convert Int/Decimal to float in C#?
How does one convert from an int or a decimal to a float in C#? 如何在C#中将int或decimal转换为float?
I need to use a float for a third-party control, but I don't use them in my code, and I'm not sure how to end up with a float. 我需要使用float来进行第三方控件,但我不会在我的代码中使用它们,而且我不确定如何最终使用float。
You can just do a cast 你可以做个演员
int val1 = 1;
float val2 = (float)val1;
or 要么
decimal val3 = 3;
float val4 = (float)val3;
The same as an int: 与int相同:
float f = 6;
Also here's how to programmatically convert from an int to a float, and a single in C# is the same as a float: 此外,这里是如何以编程方式从int转换为float,C#中的单个与float相同:
int i = 8;
float f = Convert.ToSingle(i);
Or you can just cast an int to a float: 或者你可以将一个int转换为一个浮点数:
float f = (float)i;
You don't even need to cast, it is implicit. 你甚至不需要施放,这是隐含的。
int i = 3;
float f = i;
A full list/table of implicit numeric conversions can be seen here http://msdn.microsoft.com/en-us/library/y5b434w4.aspx 隐藏数字转换的完整列表/表可以在这里看到http://msdn.microsoft.com/en-us/library/y5b434w4.aspx
它只是:
float f = (float)6;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.