Converting Textview value to double

Asked

Viewed 772 times

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

1 answer

6


There are several ways to limit the number of decimals. See below some:

String format.()

double value = 2.085;
String strValue = String.format("%.2f", value ); 

Decimalformat

double value = 2.085;
DecimalFormat form = new DecimalFormat("0.00"); 
String strValue = form.format(value);

Math

Create a method using .pow and .round:

private double limitdecimals(double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}

Use it this way: double value = 2.085; String strValue = String.valueOf(limitdecimals(value,2))

Numberformat

double value = 2.085;
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(2); // limita a quantidade de números fracionários
String strValue = format.format(value);

Remembering that the above assignments are being converted to String to be printed in TextView. If you want to do some operation, you should keep them on the type double.

Then just you assign to your TextView, using the method setText(). Behold:

tvTotalPriceItem.setText(strValue);

Exploring the Sqlite

Another legal way to do this would be to perform the procedure directly on query through your Sqlite. Then you can create a variable by inserting printf to format using the %.2f, which refers to two decimal places. See:

String valueFormat = "printf('%.2f', "+database.TOTAL_PRICE_ITEM+")" +
            " AS "+database.TOTAL_PRICE_ITEM;

See more details of the method printf() in sqlite documentation.

The method loadData() would look like this:

public Cursor loadData() {
    Cursor cursor;
     String valueFormat = "printf('%.2f', "+database.TOTAL_PRICE_ITEM+") AS "+database.TOTAL_PRICE_ITEM;
    String[] field = {database.ID, database.PRODUCT_NAME, database.COUNT, database.PRICE_UNIT, valueFormat};
    db = database.getReadableDatabase();
    cursor = db.query(database.TABLE, field, null, null, null, null, null, null);

    if (cursor != null) {
        cursor.moveToFirst();
    }
    db.close();
    return cursor;
}
  • 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.

  • @Giovanirodrigo which variable represents this set of values?

  • The variable field. I suppose she’s responsible for holding all the values that will be printed on ListView

  • This variable is like this in my database creation class public static final String TOTAL_PRICE_ITEM = "TOTAL_PRICE_ITEM"; No Sqlite column TOTAL_PRICE_ITEM holds values DECIMAL The data is entered in that column in double format.

  • 1

    Man, thank you so much, you saved my day!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.