SAN
SAN

Reputation: 979

How to set 2 decimal places in sqlite select statement

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.

    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.

Im using following code to populate the the list view in 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.

Upvotes: 1

Views: 16703

Answers (2)

Rupok
Rupok

Reputation: 2082

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

Upvotes: 5

Kennet
Kennet

Reputation: 5796

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));

Upvotes: 2

Related Questions