开发者

Problem summing floats from SQLite database for android app

开发者 https://www.devze.com 2023-03-14 23:23 出处:网络
I\'m having a problem with summing floats properly from my SQLite database. I have created my table as follows where I have set the column \'amount\' as FLOAT through the String sql4.

I'm having a problem with summing floats properly from my SQLite database. I have created my table as follows where I have set the column 'amount' as FLOAT through the String sql4.

I have an EditText 'value' where the user enters a value, then it is entered into the database as through the ContentValues cv2.

All of this works fine, when I check the database externally the decimal value is recorded. However when I try to retrieve it through the mCursor1 the output is shown as 9.0 instead of 9.18 after I made two consecutive entries of 2.21 and 6.97…….. However when I output the data into an xml file and do the addition in excel I achieve the 9.18. This would leave me to believe it must be something due to the mCursor1 but I'm only guessing. Any ideas of where my problem lies?

String sql4 = "create table tracker (_id INTEGER PRIMARY KEY " +
        "AUTOINCREMENT, category_name TEXT NOT NULL, date DATE NOT NULL, place NUMERIC NOT NULL," +
        "amount FLOAT NOT NULL, day_id INTEGER NOT NULL, week_id INTEGER NOT NULL," +
        "month_id INTEGER NOT NULL, year_id INTEGER NOT NULL)";

ContentValues cv2 = new ContentVal开发者_高级运维ues();
cv2.put("amount", value.getText().toString());
mDb.insert("tracker", null, cv2);

float total1 = 0;
            mCursor1 = mDb.rawQuery(
                    "SELECT SUM(amount) FROM tracker", null);
                if(mCursor1.moveToFirst()) {
                    total1 = mCursor1.getInt(0);
                }
            mCursor1.close();

            String a = Float.toString((float)total1);           
            displayTotal.setText("Total €" + a);


you are using

mCursor1.getInt(0);

but you should use

mCursor1.getFloat(0);


Try using mCursor1.getDouble() or mCursor1.getFloat() instead of mCursor1.getInt().


total1 = mCursor1.getInt(0); 

Here's your problem ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号