在我的应用中,允许用户上传和出售他们的商品,但是我只允许用户以美元添加价格标签,但是在他们成功上传他们想要出售的商品之后,我希望将该价格以美元转换为另一种货币美国以外的用户,因此我尝试了在另一个问题中找到的代码,但我仍无法正确理解其工作原理。
这是我尝试过的代码
public void setItemPrice(String ItemPrice) {
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.getDefault());
price = mView.findViewById(R.id.post_price);
price.setText(format.format(ItemPrice));
}
但是当我运行应用程序时,此logcat错误崩溃
java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:640)
at java.text.Format.format(Format.java:157)
这是在Firebase中格式化价格标签的方式
postPrice.setCurrency("$");
postPrice.setDelimiter(false);
postPrice.setSpacing(false);
postPrice.setDecimals(false);
这在firebase中的外观