Fetching double value from Database and calculate with them

Ashwilf1

New Member
I got another weird problem which i just don't understand.I am saving doubles into my database with max. 3 digits after the decimal point.The doubles are stored in the database correctly but when i am trying to fetch the data and add/multiply the stored doubles i get a weird problemIf i am storing data with only one digit after the decimal point, everything works fine and i get the right results. But if i am adding 2 or more digits after the decimal point my whole calculations won't work. I get no results into my TextViews.I'm trying to understand whats happening for hours now but i just don't see why this happens.Here is the code:My MainActivity, where the TextViews get the Text:\[code\]public void calc() { // Get text for the statistics amount_widget.setText(db.getSearchResult("amount", 0)); lpayment_widget.setText(db.getSearchResult("price", 0)); tpayment_widget.setText(db.getSearchResult("total_price", 1)); mileage_widget.setText(db.getSearchResult("mileage", 2)); trefueled_widget.setText(db.getSearchResult("amount", 1)); // Calculate text for the consumption double consumption = (Double.parseDouble(db .getSearchResult("amount", 1)) / Double.parseDouble(db .getSearchResult("mileage", 2))) * 100; consumption = Math.round(consumption * 100) / 100.0; consumption_widget.setText(consumption + "L /100km"); }\[/code\]My Database Activity where the code is being fetched and calculated:\[code\]public String getSearchResult(String sql, int cmd) {if (cmd == 0) { String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME + " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")"; Cursor cursor = db.rawQuery(countQuery, null); String tmp = cursor.moveToFirst() ? cursor.getString(0) : "0"; cursor.close(); return tmp;} else if (cmd == 1) { double sum = 0; String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME; String idQuery = "SELECT _id FROM " + TABLE_NAME + " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")"; Cursor cursor = db.rawQuery(countQuery, null); Cursor id = db.rawQuery(idQuery, null); // berechnung cursor.moveToFirst(); id.moveToFirst(); int maxId = Integer.parseInt(id.getString(0)); for (int i = 0; i < maxId; i++) { double tmp = Integer.parseInt(cursor.getString(0)); sum = sum + tmp; cursor.moveToNext(); } cursor.close(); id.close(); return String.valueOf(sum);} else if (cmd == 2 && sql == "mileage") { double sum = 0; String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME; String idQuery = "SELECT _id FROM " + TABLE_NAME + " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")"; Cursor cursor = db.rawQuery(countQuery, null); Cursor id = db.rawQuery(idQuery, null); // berechnung cursor.moveToFirst(); id.moveToFirst(); int maxId = Integer.parseInt(id.getString(0)); if (maxId > 1) { int array[] = new int[maxId]; // Array f
 
Top