【问题标题】:Decimal Amount Format Issue-Android小数金额格式问题-Android
【发布时间】:2015-01-08 05:29:05
【问题描述】:

声明:

DecimalFormat mAmtFormat = new DecimalFormat("##,##,##,##0.00");

edtAmounts = (EditText) findViewById(R.id.txtAmounts);

从xml文件编辑文本为

<EditText
    android:id="@+id/txtAmounts"
    android:layout_height="50dip"
    android:layout_marginLeft="10dip"
    android:background="#FFFFFF"
    android:gravity="center_vertical"
    android:inputType="numberDecimal"
    android:singleLine="true"
    android:textSize="18dip"
    android:textStyle="bold"
    android:width="170dip" />

从后端 mCurtotamt 是 565656565(double)

从 Sqlite 数据库中获取数据:

edtAmounts.setText(String.valueOf(mAmtFormat.format(mDoubleformat
                                .parse(mCurtotamt).doubleValue())))

但在编辑文本中设置的值是 56,56,57,000.00

这里发生了什么。

【问题讨论】:

  • mDoubleformat是如何初始化的?另外,告诉我们编辑文本应该如何显示。
  • edittext 应该显示为真实值,如 565656565.00
  • NumberFormat mDoubleformat = NumberFormat.getInstance(Locale.US);
  • 我试过你的代码,它对我来说很好用。也许你从后端得到了错误的值。
  • 从后端我得到这样的 5.65657e+08

标签: android decimal formatter


【解决方案1】:

当你从后端获取值时,不要像这样将它作为字符串获取:

String mCurtotamt = cursor.getString(cursor.getColumnIndex("column_name"));

相反,直接将其作为双精度获取,如下所示:

double mCurtotamt = cursor.getDouble(cursor.getColumnIndex("column_name"));

那你设置编辑文本的时候就不用解析了,直接格式化就可以了:

edtAmounts.setText(String.valueOf(mAmtFormat.format(mCurtoamt)));

在调用 cursor.getString() 时将其转换为字符串时引入了问题。

【讨论】:

  • 非常感谢。它工作正常
【解决方案2】:

将此用于印度格式,例如“##,##,##0.00”:

static public String formatCurrency(Double doubleVal) {
     return new DecimalFormat("##,##,##0.00").format(doubleVal);
}

此函数以正确的给定格式返回值。如果您传递 5555555.00 值,则函数将 55,55,555.00 作为字符串返回。

【讨论】:

  • 我已经尝试过你的代码,但它不起作用。ami我犯了任何错误,但@samgak 回答它对我有用。无论如何感谢你的帮助
猜你喜欢
  • 2020-10-05
  • 1970-01-01
  • 2019-04-16
  • 2016-12-13
  • 2021-05-20
  • 2020-10-25
  • 1970-01-01
  • 2013-09-28
  • 2019-08-18
相关资源
最近更新 更多