Vol*_*myr 0 java sqlite android
我使用真实类型来保存SQLite数据库中的价格值,但它没有正确返回值.如果我保存一些像1.01它会返回的值4.029999999999999.我已经读过在SQLite中保存价格的最好方法是使用整数类型
但我不知道如何使用它.在这种情况下如何存储和检索价格?这里编码我现在检索价格的方式:
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));
}
Run Code Online (Sandbox Code Playgroud)
当你看到它时,它实际上非常简单.
我不知道你的货币是什么,但假设是美元.
你想存储1.01美元.这与101美分相同.你只需乘以100.
因此,在存储数字之前,乘以100.显示数字时,除以100.
只是为了补充答案:我还认为你应该将数字存储在内存中作为分数/整数.因此,对于您的所有工作,计算等,请使用该整数格式.
如果用户输入$ amount,则将其直接转换为整数(乘以100).并且只在需要显示时才更改回$.
换句话说,不仅仅是在往返数据库时.
| 归档时间: |
|
| 查看次数: |
4193 次 |
| 最近记录: |