Android 使用 XML 格式化十进制数
Android Format Decimal Number using XML
我想将数字格式化为小数点后两位。我不知道为什么我的代码收到错误。
这是我的代码。
strings.xml:
<resources>
<string name="progress">%1$d / %2$d (%3.2f%%)</string>
</resources>
MainActivity.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));
}
遇到错误:
Caused by: java.util.IllegalFormatConversionException: %f can't format java.lang.Integer arguments
我想要这样的输出。 1/100 (0.01%)
替换
<string name="progress">%1$d / %2$d (%3.2f%%)</string>
和
<string name="progress">%1$d / %2$d (%3$.2f)</string>
我想将数字格式化为小数点后两位。我不知道为什么我的代码收到错误。
这是我的代码。
strings.xml:
<resources>
<string name="progress">%1$d / %2$d (%3.2f%%)</string>
</resources>
MainActivity.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));
}
遇到错误:
Caused by: java.util.IllegalFormatConversionException: %f can't format java.lang.Integer arguments
我想要这样的输出。 1/100 (0.01%)
替换
<string name="progress">%1$d / %2$d (%3.2f%%)</string>
和
<string name="progress">%1$d / %2$d (%3$.2f)</string>