如何将x.0更改为x?

时间:2013-12-07 18:21:54

标签: java

在120/10结果中,

是12.0 我希望res在浮点模式下为12 我可以将12.0更改为12吗?

Float x =120f;
Float y =10f;
res.setText(x/y);

终于res> 12.0

4 个答案:

答案 0 :(得分:3)

将其投射到int。

res.setText((int)(x/y));

答案 1 :(得分:3)

使用String.format()

String.format("%.1f", 12.0f); //returns "12,0"
String.format("%.0f", 12.0f); //returns "12"
String.format("%.1f", 120f / 10f); //returns "12,0"
String.format("%.0f", 120f / 10f); //returns "12"

另请查看this链接以获取更多信息,或this

答案 2 :(得分:1)

类似于@ ronmrdechai的回答,只有你想要对结果进行舍入,这将是类似的

int result = (int)(x/y + 0.5f);
res.setText(Integer.toString(result));

答案 3 :(得分:1)

Float x =120f;
Float y =10f;
res.setText((int)(x/y));

您必须将其投射到int。那个,或者最初是他们的类型ints / Integers