0

I want to format the number with 2 decimal places. I don't know why my code is receiving an error.

this is my code.

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));
}

Error encounter:

Caused by: java.util.IllegalFormatConversionException: %f can't format java.lang.Integer arguments

I want the output like this. 1/100 (0.01%)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
dotGitignore
  • 1,597
  • 2
  • 15
  • 34

1 Answers1

2

replace

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

with

 <string name="progress">%1$d / %2$d (%3$.2f)</string>
Ankit Aman
  • 999
  • 6
  • 15