Home » Android » Android getString(): xliff format float / double

Android getString(): xliff format float / double

Posted by: admin June 15, 2020 Leave a comment

Questions:

Howto format strings with xliff using floats and doubles (decimal places)?

<string name="test">Test <xliff:g id="float1">%1$f</xliff:g> <xliff:g id="float2">%1$.2f</xliff:g></string>

Code:

mContext.getString(R.string.test, 1.23456);
How to&Answers:

You can define the number of decimal places in the xliff placeholder individually for each number as follows:

<string name="test">
    First <xliff:g id="first_number" example="100.123">%1$.4f</xliff:g> number 
    is and second number is <xliff:g id="second_number" example="200.12">%2$.2f</xliff:g>.
</string>

Usage:

mContext.getString(R.string.test, 100.123456789, 200.123456789);

Output:

First number is 100.1234 and second number is 200.12.

Please note that you have to use numbered placeholder as here %1f, %2f, …

The placeholder structure is as follows for the example %1$.4f:

  • %1 is the serial number
  • f is the data type, float in this case
  • .4 defines the number of decimal places for the float, four in this case