Android getString(): xliff format float / double
Asked Answered
E

1

8

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);
Elka answered 16/7, 2014 at 15:54 Comment(0)
C
16

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
Calculate answered 28/10, 2014 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.