format statement in a string resource file

AndroidAndroid Resources

Android Problem Overview


I have strings defined in the usual strings.xml Resource file like this:

<string name="hello_world"> HELLO</string>

Is it possible to define format strings such as the one below

 result_str = String.format("Amount: %.2f  for %d days ",  var1, var2);

in the strings.xml resource file?

I tried escaping the special characters but its not working.

Android Solutions


Solution 1 - Android

You do not need to use formatted="false" in your XML. You just need to use fully qualified string format markers - %[POSITION]$[TYPE] (where [POSITION] is the attribute position and [TYPE] is the variable type), rather than the short versions, for example %s or %d.

Quote from Android Docs: String Formatting and Styling: > Hello, %1$s! You have %2$d new messages. > > In this example, the format string has two arguments: %1$s is a > string and %2$d is a decimal integer. You can format the string with > arguments from your application like this: > > Resources res = getResources(); > String text = res.getString(R.string.welcome_messages, username, mailCount);

Solution 2 - Android

You should add formatted="false" to your string resource


Here is an example

In your strings.xml :

<string name="all" formatted="false">Amount: %.2f%n  for %d days</string>

In your code:

yourTextView.setText(String.format(getString(R.string.all), 3.12, 2));

Solution 3 - Android

Inside file strings.xml define a String resource like this:

<string name="string_to_format">Amount: %1$f  for %2$d days%3$s</string>

Inside your code (assume it inherits from Context) simply do the following:

 String formattedString = getString(R.string.string_to_format, floatVar, decimalVar, stringVar);

(In comparison to the answer from LocalPCGuy or Giovanny Farto M. the String.format method is not needed.)

Solution 4 - Android

Quote from Android Docs: > If you need to format your strings using String.format(String, > Object...), then you can do so by putting your format arguments in the > string resource. For example, with the following resource: > > Hello, %1$s! You have %2$d new messages. > > In this example, the format string has two arguments: %1$s is a string > and %2$d is a decimal number. You can format the string with arguments > from your application like this: > > Resources res = getResources(); > String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

Solution 5 - Android

For me it worked like that in Kotlin:

my string.xml

 <string name="price" formatted="false">Price:U$ %.2f%n</string>

my class.kt

 var formatPrice: CharSequence? = null
 var unitPrice = 9990
 formatPrice = String.format(context.getString(R.string.price), unitPrice/100.0)
 Log.d("Double_CharSequence", "$formatPrice")

D/Double_CharSequence: Price :U$ 99,90

For an even better result, we can do so

 <string name="price_to_string">Price:U$ %1$s</string>

 var formatPrice: CharSequence? = null
 var unitPrice = 199990
 val numberFormat = (unitPrice/100.0).toString()
 formatPrice = String.format(context.getString(R.string.price_to_string), formatValue(numberFormat))

  fun formatValue(value: String) :String{
    val mDecimalFormat = DecimalFormat("###,###,##0.00")
    val s1 = value.toDouble()
    return mDecimalFormat.format(s1)
 }

 Log.d("Double_CharSequence", "$formatPrice")

D/Double_CharSequence: Price :U$ 1.999,90

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCocoNessView Question on Stackoverflow
Solution 1 - AndroidLocalPCGuyView Answer on Stackoverflow
Solution 2 - AndroidSherif elKhatibView Answer on Stackoverflow
Solution 3 - AndroidTimo BährView Answer on Stackoverflow
Solution 4 - AndroidGiovanny Farto M.View Answer on Stackoverflow
Solution 5 - AndroidAllanRibasView Answer on Stackoverflow