-1

I have this type of string:

<string name="temperature">%1$s °C</string>

How can i use integer in the place where variable should be instead of string? I've seen docs and found info only about string and double

4
  • stackoverflow.com/a/40715374/4729721 Commented Sep 2, 2021 at 7:57
  • To add to the above comment, decimal number identifier that is d works well with integers and if you keep your code as is, it will still work because integer will be typecasted to string within the limits of casting ofcourse Commented Sep 2, 2021 at 7:59
  • @gtxtreme i have crash when i'm using "d" with integer Commented Sep 2, 2021 at 8:05
  • You should wrap it in a try-catch construct to know exactly why this happens but I use it and it works for me normally Commented Sep 2, 2021 at 8:06

2 Answers 2

0

use this in your XML file to convert integer into a string:

<string name="my_name">Text here num %1$d</string>

and then in your java file call the XML string resource like this:

getString(R.string.my_name), getResources().getInteger(R.integer.num1)));

that way the %d gets replaced by the integer you pass in the getString.

Sign up to request clarification or add additional context in comments.

1 Comment

@Dante313 didn't my solution worked for you?
0

In Android somehow %1$d designed for Int type. %1$f designed for Double type.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.