0

I would like to display something like this:

Dear %s, You have 10% left.

Which would result in:

Dear Johnny Bravo, You have 10% left.

I am using String.format() to format the string for the persons name, but its getting confused with the % in 10% and throwing a MissingFormatArgumentException.

I have tried this

10%

10% 

but both ways throw the exception.

If i totally remove the % or % then it works fine?

How can I display the % in the text while using String.format

Example Code:

Java

texview.setText(String.format(getString(R.string.lbl_text), user.getName());

strings.xml

<string name="lbl_text">Dear %s, You have 10% left.</string>
Stillie
  • 2,647
  • 6
  • 28
  • 50
  • You didn't say what programming language you're using to display the formatted string, but in some languages, you can either escape the % (using `\%`) or put it in twice (`%%`) to get a literal percent. – lurker Apr 04 '19 at 11:09
  • String.format() to format the string for the persons name why? – primo Apr 04 '19 at 11:09
  • can you add some code so that we can get a reference of what you are actually doing – primo Apr 04 '19 at 11:10
  • im sorry, its Java, more specifically Android. I will make the change now – Stillie Apr 04 '19 at 11:24
  • Thank you, the duplicate worked fine Use double %% when you want to just display %. `Dear %s, You have 10%% left.` – Stillie Apr 04 '19 at 11:39

0 Answers0