dotGitignore
dotGitignore

Reputation: 1627

Android Format Decimal Number using XML

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%)

Upvotes: 0

Views: 2228

Answers (1)

Ankit Aman
Ankit Aman

Reputation: 1009

replace

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

with

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

Upvotes: 2

Related Questions