java 如何在sqlite select语句中设置2个小数位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7397936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:47:09  来源:igfitidea点击:

How to set 2 decimal places in sqlite select statement

javaandroidsqlite

提问by SAN

I have a sqlite table that has amount field with NUMERIC(10,5) value(I know data type meant not much in sqlite). I want to write a select statement that display amount in 2 decimal places.

我有一个带有 NUMERIC(10,5) 值的数量字段的 sqlite 表(我知道数据类型在 sqlite 中意义不大)。我想写一个选择语句,以 2 个小数位显示金额。

    id      name             type        notnull      dflt_value      pk   
------- --------------- ---------- ----------- -------------- --- 
0        _id              integer       0                            1    
1        name             varchar       1                            0    
2        amount           NUMERIC(10,5) 0                            0    

SELECT _id, name, amount FROM accounts

Is it possible to convert the amount in two decimal places within the select statement, so I can display with two decimal places in the application. I need to format in the select statement, don't wants to change the table structure. Thanks in advance.

是否可以在 select 语句中将金额转换为两位小数,以便我可以在应用程序中显示两位小数。我需要在select语句中格式化,不想改变表结构。提前致谢。

Im using following code to populate the the list view in android

我使用以下代码填充 android 中的列表视图

    cursor = dbAdapter.getAccountsTotals(); 
    startManagingCursor(cursor); 
    String[] from = new String[] { DbAdapter.KEY_ID, DbAdapter.KEY_NAME, DbAdapter.KEY_AMOUNT}; 
    int[] to = new int[] { R.id.dis1, R.id.dis2, R.id.dis3}; 
    SimpleCursorAdapter trans = new SimpleCursorAdapter(this, R.layout.accts_row, cursor, from, to); 
setListAdapter(trans); 

so can not format the results after fetching form the database. Please let me know it there is a way to assign formatted values in above code, thanks.

所以无法在从数据库中获取后格式化结果。请让我知道有一种方法可以在上面的代码中分配格式化的值,谢谢。

回答by Rupok

SELECT _id, name, ROUND(amount,2) FROM accounts

Following code will give you idea how you can do it

以下代码将让您了解如何做到这一点

    

public class TestListView extends ListActivity {
  ...
  private DecimalFormat myCustDecFormatter = new DecimalFormat("########.00");
  ...
  ...
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)  {
    ...
    ...
    ...
  }

private void fillData() {
    /* Get all of the rows from the database and create the item list */
    /* for mult accts, pass in acct name? */
    mEntryCursor = mDbHelper.fetchAllEntries();
    startManagingCursor(mEntryCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{myDbAdapter.KEY_NMBR,myDbAdapter.KEY_DATE,myDbAdapter.KEY_DESCR,myDbAdapter.KEY_AMT};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.txtnmbr, R.id.txtdate, R.id.txtdescr, R.id.txtamt};

    // Now create a simple cursor adapter and set it to display
    setListAdapter(new SimpleCursorAdapter(this, R.layout.entryrow, mEntryCursor, from, to) {
        @Override
        public void setViewText(TextView v, String text) {
          super.setViewText(v, convText(v, text));
        }

    });

  }

  private String convText(TextView v, String text) {
    switch (v.getId()) {
      case R.id.txtamt:
        double dblAmt;
        //dblAmt = Double.valueOf(text);
        dblAmt = mEntryCursor.getDouble(AMT_COLUMN);
        return myCustDecFormatter.format(dblAmt);
    }
      return text;
    } 

}//end TestListView

回答by Kennet

Leave float value as it is in the database and format in presentation layer, somthing like this:

将浮点值保留在数据库中,并在表示层中设置格式,如下所示:

 float f = 1.23456f;
    NumberFormat instance = NumberFormat.getInstance();
    instance.setMaximumFractionDigits(2);
    System.out.println(instance.format(f));