Android/SQLite:保存/检索 REAL 类型的数据 [英] Android/SQLite : Saving/Retrieving data of type REAL
问题描述
我有一个带有 REAL 字段表的 SQLDB.在我的布局中,我将其字段大小设置为仅允许 8+2 位.在我的对象中,我将该字段的数据类型用作float".
I have a SQLDB with table of field REAL. In my layout I have set its field size to allow only 8+2 digits. In my object I have used the data-type of the field as "float".
qs[0] = "CREATE TABLE " + RELATION_TABLE_NAME + "(id TEXT PRIMARY KEY, name TEXT NOT NULL, startBal REAL NOT NULL, currentBal REAL NOT NULL);";
<EditText android:text="" android:id="@+id/relCurrBalTxt" style="@style/EditTextStyle"
android:inputType="numberDecimal" android:maxLength="11" android:hint="12345678.99" />
我在 editText 中输入了值87654321.99"并点击了保存.它填充对象
I entered value "87654321.99" in my editText and clicked save. It populates the object
// Send the data to object
rowData.setValue(2, Float.parseFloat(sbalTxt.getText().toString()));
// In object, this is how I save it
this.setCurrentBalance( ((Float)value).floatValue() ); // value is Object type
// Saving data in ContenteValues to save to DB
// LOG Rcvd from Object while Saving CurrBal: 8.765432E7
values.put("currentBal", new Float(rowData.getValue(3).toString()));
保存时,它直接显示更新数据的表格.在表格中显示时,我使用 DecimalFormat 来确保它以正确的方式显示:
On saving, it directly show the table with the updated data. While showing it in table I use DecimalFormat to make sure it is shown in proper manner :
field_type = r.getFieldType(field);
// Get Data
str = r.getValue(field).toString();
// Format accordingly
if(field_type.equals(DBRow.DOUBLE_TYPE) || field_type.equals(DBRow.FLOAT_TYPE)) {
double douValue = Double.parseDouble(str);
//NumberFormat nf = NumberFormat.getNumberInstance(Locale.ITALY);
//DecimalFormat df = (DecimalFormat) nf;
DecimalFormat df = new DecimalFormat();
df.applyPattern("##,###,###.##");
df.setGroupingUsed(true);
str = df.format(douValue);
// Log.v("DF", "While Showing in Table : double = " + douValue + " String = " + str);
}
((TextView) tr.getChildAt(field)).setText(str);
这里显示了值:8.765432E7
HERE it showed me the value : 8.765432E7
当我再次选择该行以查看 EditText 中的值时,我看到:87654320.00
When I again selected that row to see the values in EditText I see : 87654320.00
值如何变化?在其他情况下,我也将数据保存为50009876.99",但不知何故,它没有保存 .99,而是将其保存为 .00,即 50009876.00.
How is the value changed ? In other instance also I saved the data as "50009876.99", but somehow it doesn't save .99 and makes that as .00 i.e 50009876.00.
为什么事情不能正常工作?我哪里错了?
Why things are not working correctly ? Where am I going wrong ?
非常感谢任何帮助.
*用于在表中显示的 DecimalFromat 代码 *
// Format accordingly
if(field_type.equals(DBRow.DOUBLE_TYPE) || field_type.equals(DBRow.FLOAT_TYPE)) {
/*
WAS USING THIS CODE TILL NOW
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
String decep = String.valueOf(dfs.getDecimalSeparator());
int index = str.indexOf(decSep);
if (index > 0) {
// If digits after decimal are more than 2
if (str.substring(index+1).length() > 2) {
str = str.substring(0, index+3);
}
}
DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(true);
str = df.format(Double.parseDouble(str));
if (addRow)
create_addTextView(tr, str, true);
else
((TextView) tr.getChildAt(field)).setText(str);
*/
// DECIMALFORMAT CODE
double douValue = Double.parseDouble(str);
DecimalFormat df = new DecimalFormat();
df.applyPattern("##,###,###.##");
df.setGroupingUsed(true);
str = df.format(douValue);
if (addRow)
create_addTextView(tr, str, true);
else
((TextView) tr.getChildAt(field)).setText(str);
}
推荐答案
我认为您的问题是您将数字存储为 Float
.Float
没有很好的精度 - 它实际上只有 23 位二进制精度,如 这里.这意味着您无法在 float
中准确存储小数点前的 7 位,甚至更糟 - 小数点前的 7 位和小数点后的几位.这意味着你错误地选择了 balance 变量的类型——你不能存储在 Float
8+2 中.我建议您将类型更改为 Double
,它的精度范围要大得多.
I think your problem is that you store the numbers as Float
. Float
does not have very good precision - it actually has only 23 binary digits of precision as seen here. This means that you can not store accurately in float
7 digits before the decimal point or even worse - 7 digits before the point and a couple after. This means that you have incorrectly chosen the type of the balance variable - you can not store in Float
8+2. I would recommend you to change the type to Double
, which has significantly larger range of precision.
这篇关于Android/SQLite:保存/检索 REAL 类型的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!