5
Good afternoon, on my project I’m having the following problem.
In it you have an Activity in which products are added. To add a product you need to fill the fields Name, Quantity and Unit Value.
The number and unit value fields are multiplied and the result is added in the column TOTAL_PRICE_ITEM
in my database.
When doing the multiplication depending on the values, it is returning 3 decimal places
Ex: 1.5 * 1.39 = 2.085 (2 integers and 85 thousandths)
Since I want you to return only 2.08 (2 whole and 8 hundredths)
These values are rescued from the database through the onload function in the class Controller
Controller.class (loadData)
public Cursor loadData() {
Cursor cursor;
String[] field = {database.ID, database.PRODUCT_NAME, database.COUNT, database.PRICE_UNIT, database.TOTAL_PRICE_ITEM};
db = database.getReadableDatabase();
cursor = db.query(database.TABLE, field, null, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
db.close();
return cursor;
}
And printed on a Listview in my class Main
Controller crud = new Controller(getBaseContext());
final Cursor cursor = crud.loadData();
String[] field = new String[]{CreateDB.ID, CreateDB.PRODUCT_NAME, CreateDB.COUNT, CreateDB.PRICE_UNIT, CreateDB.TOTAL_PRICE_ITEM};
int[] idViews = new int[]{R.id.tvId, R.id.tvProductName, R.id.tvCount, R.id.tvPriceUnit, R.id.tvTotalPriceItem};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.group_layout, cursor, field, idViews, 0);
list = (ListView) findViewById(R.id.elvProductList);
list.setAdapter(adapter);
The variable String[] field
I suppose I’m responsible for keeping the values coming from the function loadData
.
The variable int[] idViews
I assume you are responsible for taking the values stored in the variable String[] field
and put each in its place.
The value that is coming with 3 decimal places is being displayed on TextView
id tvTotalPriceItem
.
I want to know how to convert a specific value that is in a set of values.
I don’t know if I was too clear about my doubt.
Att.
Giovani Rodrigo
Right, the formatting part I understood, but how do I format a value that is in a group of values? As you can see the value to be formatted is in the group
String[] field
.– Giovani Rodrigo
@Giovanirodrigo which variable represents this set of values?
– viana
The variable
field
. I suppose she’s responsible for holding all the values that will be printed onListView
– Giovani Rodrigo
This variable is like this in my database creation class
public static final String TOTAL_PRICE_ITEM = "TOTAL_PRICE_ITEM";
No Sqlite columnTOTAL_PRICE_ITEM
holds valuesDECIMAL
The data is entered in that column in double format.– Giovani Rodrigo
Man, thank you so much, you saved my day!
– Giovani Rodrigo
Let’s go continue this discussion in chat.
– Giovani Rodrigo