Android strings.xml中点后带2位数字的双参数?

Android strings.xml中点后带2位数字的双参数?,android,string,android-resources,formatter,Android,String,Android Resources,Formatter,我希望在strings.xml中的一个字符串中包含一个参数,并且该参数应为双精度值。所以我使用%1$f。这里有很多例子,但是如果我想有一些双/浮点参数,而我只想第二个参数在之后有两位数,该怎么办?我尝试使用像%2$.2f或%2.2$f这样的组合。他们中的任何一个都不起作用%.1f也不起作用。 那么,有人知道如何在strings.xml中“定制”一个float/double值吗?谢谢。如果是我,我会将这些值作为简单值存储在资源中,然后使用格式化程序方法控制它们的显示方式,大致如下 public S

我希望在
strings.xml
中的一个字符串中包含一个参数,并且该参数应为双精度值。所以我使用
%1$f
。这里有很多例子,但是如果我想有一些
双/浮点
参数,而我只想第二个参数在
之后有两位数,该怎么办?我尝试使用像
%2$.2f
%2.2$f
这样的组合。他们中的任何一个都不起作用<代码>%.1f也不起作用。
那么,有人知道如何在
strings.xml
中“定制”一个
float/double
值吗?谢谢。

如果是我,我会将这些值作为简单值存储在资源中,然后使用格式化程序方法控制它们的显示方式,大致如下

public String formatFigureTwoPlaces(float value) {
    DecimalFormat myFormatter = new DecimalFormat("##0.00");
    return myFormatter.format(value);
}

public String formatFigureOnePlace(float value) {
    DecimalFormat myFormatter = new DecimalFormat("##0.0");
    return myFormatter.format(value);
}

我现在知道这个答复来得太晚了。。。但我希望能够帮助其他人:

当您需要十进制数字并将其格式化为通用样式%a.bf时,Android会使用多个参数替换

我找到的最好的解决方案(仅适用于此类资源)是将十进制参数作为字符串%n$s,并在代码中使用String.format(…)应用我的转换



例如:


不正确的方式:

//在xml文件中:

<string name="resource1">You has a desviation of %1$s and that is a %2$.2f%% percentage.</string>

只是在这里添加@David Airam的答案;他给出的“不正确”的解决方案实际上是正确的,但需要稍加调整。XML文件应包含:

<string name="resource1">Hello string: %1$s, and hello float: %2$.2f.</string>
@David Airam报告的例外情况是,试图将
字符串
插入带有
%f
的格式说明符,这需要浮点类型。使用
float
,没有这种例外


此外,如果输入数据最初是字符串(例如,来自
EditText
或其他内容),则可以使用
Float.valueOf()
将字符串转换为Float。但是,您应该始终尝试/catch
valueOf()
操作并处理
NumberFormatException
情况,因为此异常未被选中。

%.1f如果您希望在“,”之后仅显示1位,则适用于我。

Define is strings.xml文件

  <string name="price_format">$%,.2f</string>

//For using in databinding  where amount is double type
    android:text="@{@string/price_format(model.amount)}"

//For using in java runtime where priceOfModifier is double type
                amountEt.setText(context.getResources().getString(R.string.price_format, priceOfModifier));
$%,.2f
//用于金额为双精度类型的数据绑定
android:text=“@{@string/price_格式(model.amount)}”
//用于java运行时,其中priceOfModifier是双精度类型
setText(context.getResources().getString(R.string.price_格式,priceOfModifier));

这里的评论才是真正的答案!你应该把它作为这个问题的另一个答案。Iomza在评论中的回答是真正的解决方案在你的“错误”示例中,
spercentage
的类型是什么?我猜(正如@Nik Reiman指出的)它是一个
字符串
——也就是说,难怪您在尝试将其作为浮点传递时(在
%2$.2f
)会遇到异常。这个“答案”是不完整和误导性的。我也不能从字符串资源获得格式化的浮点,是的,我正在传递一个浮点。我在字符串资源中使用了“%1$0.6f”,得到了一个异常:java.util.MissingFormatWidthException:%01$.6f,因此它不知怎么被弄乱了。这个答案是正确的。
...

  // This is the auxiliar line added to solve the problem
  String spercentage = String.format("%.2f%%",percentage);

  // This is the common code where we use the last variable.
  String sresult = getString(R.string.resource1, svalue, spercentage);
<string name="resource1">Hello string: %1$s, and hello float: %2$.2f.</string>
String svalue = "test";
float sfloat= 3.1415926;
String sresult = getString(R.string.resource1, svalue, sfloat);
  <string name="price_format">$%,.2f</string>

//For using in databinding  where amount is double type
    android:text="@{@string/price_format(model.amount)}"

//For using in java runtime where priceOfModifier is double type
                amountEt.setText(context.getResources().getString(R.string.price_format, priceOfModifier));