Android getString():xliff格式float / double

如何使用浮动和双精度(小数位)xliff格式化string?

<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); 

Solutions Collecting From Web of "Android getString():xliff格式float / double"

您可以在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是数据types,在这种情况下是float
  • .4定义浮点数的小数位数,在这种情况下为四位