Maria Georgali Maria Georgali - 7 months ago 35
Java Question

Why I cannot format a double number in Android

I do a calculation between an integer and double and I get a number of 2.3333 and I use the following

txtList.get(code-1).setText(String.format("%.2f",String.valueOf(FinalPPP)));


to format it but I get an error of

java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments

Answer

The problem is that ".2f" expects a double but you're giving it a String.

Just remove the String.valueOf() and it should work. Like this:

txtList.get(code-1).setText(String.format("%.2f", FinalPPP));