2011-09-13 24 views
0

我有一个sqlite表,其中有数量字段与NUMERIC(10,5)值(我知道数据类型意味着没有太多的sqlite)。我想编写一个select语句,显示两位小数的金额。如何在sqlite select语句中设置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 

是否可以在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); 

所以列表视图形式获取数据库后无法格式化的结果。请让我知道它有一种方法来分配上述代码中的格式化值,谢谢。

+0

看到这个[这里](http://stackoverflow.com/questions/3846361/decimal-places-problem-with-sqlite) –

+0

请不要尝试做数据层中的演示文稿(如格式)。尝试分离问题:从数据库中获取“数据”(不是表示具有某种精度的数字的文本字符串),然后在UI代码(page/window/report/etc)中显示格式化的“字符串”。正如@Kennet所说的。 – helios

+0

[这里](http://sqlite.awardspace.info/syntax/sqlitepg04.htm)给出了在将值插入到表中时设置两个小数点值的方法。 –

回答

4
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 
+0

查询+1;但是,如我所想,格式化表示层中的数字会更好。 – Naved

+0

感谢您的回答,但我需要有两位小数。不只是四舍五入到小数点后两位。例如123.40或56.00在Android应用程序中显示为货币值。顺便说一下,数量值是一种数字(10,5)不浮动(我的错误) – SAN

+0

Im使用以下代码来填充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); \t \t \t setListAdapter(trans);所以不能格式化结果。请让我知道有一种方法可以将格式化值分配给上述代码,谢谢。 – SAN

2

离开浮点值,因为它是在数据库和格式表示层,财产以后这样的:

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

我不同意。这种格式的合理位置是在数据库视图中。这种方法是DRYer。 –