如何使用浮动和双精度(小数位)格式化xliff的字符串?
<string name="test">Test <xliff:g id="float1">%1$f</xliff:g> <xliff:g id="float2">%1$.2f</xliff:g></string>
代码:
mContext.getString(R.string.test, 1.23456);
答案 0 :(得分:13)
您可以为每个号码单独定义xliff
占位符中的小数位数,如下所示:
<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>
用法:
mContext.getString(R.string.test, 100.123456789, 200.123456789);
输出:
First number is 100.1234 and second number is 200.12.
请注意,您必须使用编号占位符%1f
,%2f
,...
示例%1$.4f
:
%1
是序列号f
是数据类型,在这种情况下为float .4
定义浮点数的小数位数,本例中为4