Android 使用 XML 格式化十进制数



我想用 2 位小数格式化数字。我不知道为什么我的代码收到错误。

这是我的代码。

字符串.xml

<resources>
    <string name="progress">%1$d / %2$d (%3.2f%%)</string>
</resources>

主要活动.java:

int progressCount;
int totalCount = 3000;
double percentage;
for (int x = 0; x < totalCount; x++) {
   progressCount = x;
   percentage = ((((double) progressCount) / ((double) totalCount)) * 100);
   Log.i("MainActivity", "Percentage: " + String.format(getString(R.string.progress), progressCount, totalCount, percentage));
}

遇到错误:

原因:java.util.IllegalFormatConversionException: %f 无法格式化 java.lang.Integer 参数

我想要这样的输出。1/100 (0.01%)

替换

 <string name="progress">%1$d / %2$d (%3.2f%%)</string>

 <string name="progress">%1$d / %2$d (%3$.2f)</string>