【发布时间】:2014-02-13 17:25:39
【问题描述】:
我需要有关我的 android 应用程序中的某个功能的帮助。该函数获得这样的双精度:0.1234567 或 123.1234567;我想将其转换为字符串,稍后,如果双精度大于 1,则必须返回 123.1(如果双精度为 123.123456),如果双精度小于 0,则必须返回 123(如果双精度为 0.123456)。目前,我设法将双精度转换为字符串,但我不知道该怎么做。
这是我的方法:
public String getFormatDistance(double distance) {
String doubleAsText = String.valueOf(distance);
if (distance > 0.9) {
return doubleAsText;
} else {
String[] parts = doubleAsText.split(".");
String part1 = parts[0];
String part2 = parts[1];
//part2 = part2.split("");
return part2;
}
}
这行显示字符串:
TextView fourText = (TextView) row.findViewById(R.id.distance);
fourText.setText(getFormatDistance(values.get(position).distance));
它返回下一个错误:
01-23 09:45:48.816: E/AndroidRuntime(8677): java.lang.ArrayIndexOutOfBoundsException: length=0; index=1
【问题讨论】: