簡體   English   中英

如何在sqlite select語句中設置2個小數位

[英]How to set 2 decimal places in sqlite select statement

我有一個sqlite表,其數量字段具有NUMERIC(10,5)值(我知道在sqlite中數據類型的意義不大)。 我想編寫一條顯示金額的小數點后2位的select語句。

    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

是否可以在select語句中將金額轉換為兩位小數,所以我可以在應用程序中顯示兩位小數。 我需要在select語句中設置格式,不想更改表結構。 提前致謝。

我正在使用以下代碼填充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); 

因此在從數據庫中獲取數據后無法格式化結果。 請讓我知道,有一種方法可以在上面的代碼中分配格式化的值,謝謝。

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

以下代碼將使您了解如何做到這一點

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

將float值保留在數據庫中,並在表示層中設置格式,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM