简体   繁体   English

如何将x.0更改为x?

[英]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.

相关问题 如何在 x 秒内更改 JLabel 文本 - How to change a JLabel text for x Seconds 撒克逊人(Saxon)XSLT-Transformation:如何从中更改空标签的序列化 <x/> 至 <x></x> ? - Saxon XSLT-Transformation: How to change serialization of an empty tag from <x/> to <x></x>? Java如何处理表达式x = x - (x = x - 1)? - How does Java process the expression x = x - (x = x - 1)? 如何将上传的图像从 text/x-genric 更改为 image/x-genric - How to change uploaded image from text/x-genric to image/x-genric Hibernate 4.3.x和4.2.x之间是否在对抽象实现的类进行水合的方式上有所变化 - Is there a change between Hibernate 4.3.x and 4.2.x in how they hydrate abstract implemented classes 我如何在本地的 getArrow() 处更改 if(ab) 中 x 和 y 的值 - how can i change the value of the x and y in if(ab) at getArrow() locally 如何正确更改Variable x的可见性? - How can i change the visibility of my Variable x right? 如何在 Vert.x (java) 中更改默认 session 超时 - How to change the default session timeout in Vert.x (java) 如何使事件更改变量x? - How do I make my event change my variable x? 如何更改 Mac OS X 启动板中显示的 Java 应用程序名称 - How to change the Java application name shown in the Mac OS X launchpad
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM