用小数点格式化整数 [英] Formatting Integer with a decimal point
本文介绍了用小数点格式化整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个整数值,通过蓝牙从PLC设备读取,第一个数字代表一位小数。例如:100必须格式化为10.0。另外一个例子:
500 - > 50.0
491 - > 49.1
455 - > 45.5
以下行可以使用:
(Text2,String.format(%。1f,(float)(mArray [18]& 0xFF | mArray [19])
但是...还有另外一种方法可以使用 String。
$ b
x = x.substring(0,x.length() - 1)+ + x.substring(x.length() - 1);
I have an integer value that is read from a PLC device via bluetooth and the first digit represent one decimal place. For example: 100 must be formatted to 10.0. Another examples:
500 -> 50.0
491 -> 49.1
455 -> 45.5
The following line will make it okay:
data11.put("Text2", String.format("%.1f", (float)(mArray[18] & 0xFF | mArray[19] << 8) / 10.0));
But... Is there another way to do the same using String.format without divide by 10.0?
Thank you
解决方案
How about the following way?
x = x.substring(0, x.length() - 1) + "." + x.substring(x.length() - 1);
这篇关于用小数点格式化整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文