[英]How change x.0 to x?
in 120/10 result is 12.0 I want res to be 12 in float mode can I change 12.0 to 12? 在120/10结果是12.0我想在浮动模式下res为12可以将12.0更改为12吗?
Float x =120f;
Float y =10f;
res.setText(x/y);
finally res> 12.0 最后res> 12.0
将它转换为int。
res.setText((int)(x/y));
Use String.format()
使用
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"
also check this link for further information, or this . 另请查看此链接以获取更多信息,或者此信息。
Similar to @ronmrdechai's answer, only if you want to round the result, which will be something like 类似于@ ronmrdechai的答案,只有你想要对结果进行舍入,这将是类似的结果
int result = (int)(x/y + 0.5f);
res.setText(Integer.toString(result));
Float x =120f;
Float y =10f;
res.setText((int)(x/y));
You have to cast it to an int
. 你必须将它转换为
int
。 That, or make their types ints
/ Integers
originally. 那,或者最初使他们的类型为
ints
/ Integers
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.