Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView to displays a number to two decimal places only through XML [duplicate]

Tags:

android

I am populating a ListView using TextView as a row through SimpleCursorAdapter.The numbers in the TextView are displayed as say 8 or 2.6786 etc.I want them to be displayed as 8.00 or 2.67 etc.Can it be done through XML(like input Type method etc).Please suggest a wayout. The question is bit different as under: The codes are:

     // THE DESIRED COLUMNS TO BE BOUND
columns = new String[] { slStock.KEY_SCRIPT, getColumnName(2),c.getColumnName(3)};
 // THE XML DEFINED VIEWS FOR EACH FIELD TO BE BOUND TO
to = new int[] { R.id.tvstockscrpt, R.id.tvqty ,R.id.tvstockrate};
// CREATE ADAPTER WITH CURSOR POINTING TO DESIRED DATA

SimpleCursorAdapter cursAdapter = new SimpleCursorAdapter(this,R.layout.rowstock,   c,columns, to); 

   lvstockdisplay.setAdapter(cursAdapter);

Here you will notice that the cursor c fetches data (from database query in background) and populates it in the ListView directly.The question is how can I format the cursor data for say the TextView whose id is(R.id.tvstockrate) and which is populated by c.getColumnNames(3).At which point in the codes the format statement can be inserted.

like image 380
AAg Avatar asked Oct 13 '13 16:10

AAg


Video Answer


1 Answers

What about formatting the text when you set it?

yourTextView.setText(String.format("%.2f", value));
like image 112
ssantos Avatar answered Sep 18 '22 22:09

ssantos