Android 对 ListView 的项目使用 NumberFormat

发布于 2024-12-04 07:30:29 字数 1164 浏览 4 评论 0原文

我在 Edittext 中有一个字符串,我将其转换为浮点数,如下所示:

float montantFac = Float.valueOf(etMontantFac.getText().toString());

我将其保存在数据库中,稍后,我从数据库取回它,然后再次将其分配给此 EditText。

etMontantFac.setText(facture.getString(facture.getColumnIndexOrThrow("montant_fa"))); 

我的问题是它显示一些带有指数的数字。 例如,1000000 显示如下:1e+06

我怎样才能只显示数字(没有字母或+或其他任何东西)的值?

编辑:

@Pratik:谢谢,效果很好!

我还需要对 ListView 的项目执行相同的操作,但我不知道如何在这种情况下使用您的解决方案。你能帮助我吗 ?

这是我的 ListView:

    private void fillData(){
    Cursor cuFactures = db.recupListeFacture(triFactures, argumentWhereVhc, choixVhc, argumentWhereGar, choixGar);
    startManagingCursor(cuFactures);

    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.facture_ligne,
            cuFactures,
            new String[] {"libelle_fa", "nom_vhc", "montant_fa", "nom_garage", "date_fa"},
            new int[] {R.id.listeNomFac, R.id.listeNomVhcFac, R.id.listeMontantFac, R.id.listeNomGarFac, R.id.listeDateFac});

    setListAdapter(adapter);

相关字段始终为“montant_fa”

提前谢谢您!

I have a String in an Editext that I convert to a float like this :

float montantFac = Float.valueOf(etMontantFac.getText().toString());

I save it in a database and later, I get it back from the DB and I assign it again to this EditText.

etMontantFac.setText(facture.getString(facture.getColumnIndexOrThrow("montant_fa"))); 

My problem is that it displays some numbers with exponent.
For example, 1000000 is displayed like this : 1e+06

How can I do to ever display the values with numers only (no letter or + or anything else) ?

EDIT :

@Pratik : Thanks, that works fine!

I also need to do the same for an item of a ListView but I don't find how to use your solution in this case. Can you help me ?

Here is my ListView :

    private void fillData(){
    Cursor cuFactures = db.recupListeFacture(triFactures, argumentWhereVhc, choixVhc, argumentWhereGar, choixGar);
    startManagingCursor(cuFactures);

    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.facture_ligne,
            cuFactures,
            new String[] {"libelle_fa", "nom_vhc", "montant_fa", "nom_garage", "date_fa"},
            new int[] {R.id.listeNomFac, R.id.listeNomVhcFac, R.id.listeMontantFac, R.id.listeNomGarFac, R.id.listeDateFac});

    setListAdapter(adapter);

The concerned field is always "montant_fa"

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2024-12-11 07:30:29

检查此链接的格式

http://download.oracle.com/javase/ tutorial/i18n/format/decimalFormat.html

尝试这种方式

float val = facture.getFloat(facture.getColumnIndexOrThrow("montant_fa"));

NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
etMontantFac.setText(output);

check this link for format

http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

try this way

float val = facture.getFloat(facture.getColumnIndexOrThrow("montant_fa"));

NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
etMontantFac.setText(output);
丢了幸福的猪 2024-12-11 07:30:29

我知道这是旧的,但这里是 sqlite 和 listview 之间的格式数据 如果有人试图做同样的事情。我有类似的问题并用它解决了。

这是我的例子

adapter = new SimpleCursorAdapter(....);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
               if (columnIndex == cursor.getColumnIndex("columnname")) {
                   TextView tv = (TextView) view;
                   NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
                   tv.setText(nf.format(cursor.getInt(columnIndex)));
                   // must return true to be applied
                   return true;
               }
               return false;
        }
});

I know this is old, but here is for the format data between sqlite and listview in case someone trying to do the same thing. I had similar problem and solved it with that.

here is my example

adapter = new SimpleCursorAdapter(....);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
               if (columnIndex == cursor.getColumnIndex("columnname")) {
                   TextView tv = (TextView) view;
                   NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
                   tv.setText(nf.format(cursor.getInt(columnIndex)));
                   // must return true to be applied
                   return true;
               }
               return false;
        }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文