将int变量转换为float

时间:2015-11-29 17:02:42

标签: c floating-point type-conversion

所以我有一个功课要问我"你如何将int转换为浮动?"。

Google搜索给了我很多不同的结果,最有希望的是Type Casting。 但我不确定的是我如何使变量本身成为一个来自int的浮点数。

即,我有int n=5,并希望稍后对其进行转换,以便将其视为浮点数,直到代码另行指定为止。我怎样才能做到这一点? 我可以这样做:n = (float)n吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

你可以通过类型转换将int type var转换为float type val,并且也可以浮动到int。这是转换数据类型的最佳方式。

for int to float =>

int a = 6;
float b = (float)a;

和浮动到int =>

float a = 6.0;
int b = (int)a;

答案 1 :(得分:1)

你可以使用类型转换

int x=11;
float = (float)x;
相关问题