在strings.xml中使用带有2位小数的双参数?

68
我希望在 strings.xml 中的一个字符串中添加一个参数,这个参数应该是一个双精度浮点数。因此我使用了 %1$f。 在这里 - http://developer.android.com/reference/java/util/Formatter.html 有许多例子,但是如果我想要有几个double / float 参数,我只想让第二个参数在 . 后有 2 位数字,应该怎么办? 我尝试使用像 %2$.2f%2.2$f 这样的组合。但它们都不起作用。 %.1f 也不行。 那么,有人知道如何在 strings.xml 中 "自定义" float / double 值吗?谢谢。
7个回答

177

在此补充一下 @David Airam 的答案;他提供的 "错误" 解决方案实际上是正确的,只需要稍作修改。XML 文件应包含:

<string name="resource1">Hello string: %1$s, and hello float: %2$.2f.</string>

现在在Java代码中:

String svalue = "test";
float sfloat= 3.1415926;
String sresult = getString(R.string.resource1, svalue, sfloat);

@David Airam报告的异常是由于尝试使用%f格式说明符将String强制转换为浮点类型。使用float就没有这样的异常。

此外,您可以使用Float.valueOf()将字符串转换为浮点数,以便在输入数据最初是字符串(例如来自EditText或其他地方)的情况下进行转换。但是,您应该始终尝试捕获valueOf()操作并处理NumberFormatException情况,因为此异常未经检查。


9

如果你只想显示','后面的1个数字,那么%.1f就适合你。


4

定义是在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));

1
一个更简单的方法: %.2f%n float sfloat= 3.1475926; String sresult = getString(R.string.decimalunit, sfloat);
输出: 3.15

1

这对我有效。

<string name="market_price">Range%1$.0f -%2$.0f</string>

android:text="@{@string/market_price(viewModel.suggestedPriceRange.max, viewModel.suggestedPriceRange.min)}"

输出:价格范围 ₹500 - ₹1000

₹%1$.0f中,.0f表示你想要小数点后的位数。


-6
如果是我,我会将值存储在资源中作为简单的值,然后使用格式化方法来控制它们的显示,大致如下。
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);
}

2
这里的评论才是真正的答案!你应该将它作为另一个回答放在这个问题下面。我差点没看到它。 - accordionfolder
Iomza 在评论中的回答是真正的解决方案。 - Kachi

-6

我知道这个回复来得太晚了...但我希望能帮助其他人:

当你想要小数点并以常见的样式%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>

// 而在 Java 文件中

  String sresult = getString(R.string.resource1, svalue, spercentage); // <-- exception!

这个解决方案在技术上是正确的,但由于安卓替换资源系统的原因,最后一行会生成一个异常。



正确的方式/解决方案:

只需将第二个参数转换为字符串即可。

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

现在在代码中:

...

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

1
你“不正确”的例子中spercentage的类型是什么?我猜测(正如@Nik Reiman指出的那样),它是一个String - 也就是说,当尝试将其作为浮点数传递时(在%2$.2f处)你会遇到异常。这个“答案”是不完整和误导的。 - Magnus
我也无法从字符串资源中获取格式化的浮点数,而且我确实传递了一个浮点数。我在字符串资源中使用"%1$0.6f",但是我得到了一个异常:java.util.MissingFormatWidthException:%01$.6f,所以它被某种方式搞砸了。这个答案是正确的。 - Aaron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接