Converting TextView value to double

5

Good afternoon, in my project I'm having the following problem. At the same time there is an activity in which products are added. To add a product, fill in the Name, Quantity and Unit Value fields. The quantity and unit value fields are multiplied, and the result is added in the TOTAL_PRICE_ITEM column in my database. When doing multiplication depending on values, you are returning 3 decimal places

Ex: 1.5 * 1.39 = 2.085 ( 2 integers and 85 thousandths )
Being that I want to return only 2.08 ( 2 integers and 8 cents )

These values are retrieved from the database through the onLoad function that is in the 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 in 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 suppose I'm responsible for saving the values coming from the loadData function.
The variable int[] idViews suppose that it is responsible for taking the values stored in the variable String[] field and putting each one in its respective place. The value that is coming with 3 decimal places is being displayed in TextView of id tvTotalPriceItem . I want to know how to convert a specific value that is in a set of values. I do not know if I was very clear about my doubt.

Att.
Giovani Rodrigo

    
asked by anonymous 02.04.2017 / 19:11

1 answer

6

There are several ways to limit the number of decimal places. Here are a few:

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 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 in type double .

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

tvTotalPriceItem.setText(strValue);

Exploring 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 %.2f , which will refer to two decimal places. See:

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

See more details on the printf() method in SQLite documentation .

The loadData() method 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;
}
    
02.04.2017 / 20:40