【问题标题】:Printing a double value with 2 decimals with a . not a , [duplicate]打印带有 2 位小数的双精度值和 .不是,[重复]
【发布时间】:2018-07-02 06:23:24
【问题描述】:

我正在开发的应用程序有一个小问题。我正在使用一些 double 值,我想将它们放在一些 TextViews 中,但是当放置数字时,而不是“。”它出现 ”,”。我想要一个点,这样我就可以使用substring 导入值。

DecimalFormat precision = new DecimalFormat("0.00");
final TextView totalPrice = (TextView) getView().findViewById(R.id.actualPrice);
double price = 200.12357123;
totalPrice.setText("$" + precision.format(price));

TextView 内部会出现 200,12 而我想出现 200.12,所以我可以稍后将文本转换为双精度值。

【问题讨论】:

  • 但是您的代码在我的项目中运行,只是删除了 getView()。
  • @DileepPatel DecimalFormat 创建的结果取决于用户特定的Locale
  • 好的,感谢您的澄清。
  • Localedot as decimal separator 设置为一

标签: java android textview


【解决方案1】:

试试这个

Locale currentLocale = Locale.getDefault();
String formatString = "#,###,###,###.##";
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

double price = 200.12357123;

Log.i("TAG", "" + "$ " + df.format(price));

输出

【讨论】:

  • 相同的结果 mate :(
  • 这是操作已经做的。请仔细阅读问题。
  • @Andy95 请检查我的答案。
  • @devpuh 感谢您的评论。
【解决方案2】:

使用下面的代码,它会给出你想要的完全相同的答案:

double price = 200.12357123;
String s=String.format(Locale.US, "$%.2f", price);

【讨论】:

    【解决方案3】:

    您可以更改“。”用 ',' ,当你想转换成双精度。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    相关资源
    最近更新 更多