2011-09-13 17 views
0

数値フィールド(10,5)の数値フィールドを持つsqliteテーブルがあります(データ型はsqliteではあまり意味がありません)。小数点以下2桁で表示する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ステートメント内の2つの小数点以下の金額を変換することが可能ですので、私はアプリケーション内の小数点以下を表示することができます。 私はselectステートメントでフォーマットする必要がありますが、テーブル構造を変更したくありません。前もって感謝します。データベースフォームをフェッチした後、結果をフォーマットすることができないので、

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)は、テーブルに値を挿入しているときに2つの小数点の値を設定するために与えられます。 –

答えて

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

答えをありがとうが、小数点以下2桁が必要です。小数点第2位を四捨五入するだけではありません。たとえば123.40または56.00をアンドロイドアプリケーションで金額として表示します。ちなみにamount値は浮動小数点型(私の間違い)ではない数値型(10,5)です – SAN

+0

次のコードを使用して、リストビューにandroid cursor = dbAdapter.getAccountsTotals(); startManagingCursor(カーソル); 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値それは、プレゼンテーション層でのデータベースおよびフォーマットであるとして、このような気にいら:コードに続いて

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

私は同意しません。この種の書式設定のための合理的な場所はデータベースビューです。このアプローチはDRYerです。 –

関連する問題