Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getString(): xliff format float / double

Tags:

android

xliff

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);
like image 390
Eun Avatar asked Jul 16 '14 15:07

Eun


1 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
like image 59
JJD Avatar answered Sep 24 '22 13:09

JJD