0

我目前在我的 TextView 中显示浮点数,但逗号后显示了 8 个数字。有这么多数字很烦人。我不想改变我的 TextView 宽度。有谁知道如何选择逗号后显示多少个数字?或者我如何正确地将浮点数舍入以在逗号后获得 1 个数字?

4

2 回答 2

1

我发现了一些不错的东西:如果你有一个包含数字的字符串

String numberLambda = "1980507120.9475";

您可以使用 DecimalFormat 类

DecimalFormat formatter = new DecimalFormat("#,###,###.0");

这定义了如何显示 NUMBERS。

现在您可以通过这种方式将您的字符串转换为数字以在其上使用格式化程序:

double stringIntoNumber = Double.parseDouble(numberLambda);

您可以再次将其添加到字符串中以在您的 TextView 中显示它!

String newNumber = "Value formatted :" + formatter.format(numberLambda);
于 2013-05-23T13:16:14.720 回答
0

使用字符串格式化程序。看这里

例如,如果您想将 int 限制为 2 位并转换为字符串,您可以执行以下操作

String s = String.format("%2d",name);
于 2013-05-23T12:57:59.400 回答