-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

Dante313
  • 161
  • 1
  • 14
  • https://stackoverflow.com/a/40715374/4729721 – a_local_nobody Sep 02 '21 at 07: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 – gtxtreme Sep 02 '21 at 07:59
  • @gtxtreme i have crash when i'm using "d" with integer – Dante313 Sep 02 '21 at 08: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 – gtxtreme Sep 02 '21 at 08:06

2 Answers2

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.

Absar Ahmad
  • 81
  • 10
0

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

Dante313
  • 161
  • 1
  • 14