I am using real type to save price value in SQLite database but it does not return correctly value. If I save a few values like 1.01
it will return 4.029999999999999
.
I have read that best way to save prices in SQLite is to use integer type
But I don't know how to use it. How to store and retrieve prices in this case? Here code how I retrieve price now:
Cursor cursor = null;
double totalPrice = 0;
try {
cursor = mDB.rawQuery("select " + Expense.PRICE + " from " + Expense.TABLE_NAME + " where " + " (strftime("
+ dateType + ", " + Expense.DATE + "))" + "= ? ", new String[] { currentDate });
} catch (Exception e) {
Log.e(TAG, "error" + e.toString());
e.printStackTrace();
}
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
totalPrice += cursor.getDouble(cursor.getColumnIndex(Expense.PRICE));
}