Tvd*_*Tvd 2 sqlite floating-point formatting android
我有一个带字段REAL的SQLDB.在我的布局中,我将其字段大小设置为仅允许8 + 2位数.在我的对象中,我使用了字段的数据类型为"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" />
Run Code Online (Sandbox Code Playgroud)
我在editText中输入值"87654321.99",然后单击"保存".它填充对象
// 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()));
Run Code Online (Sandbox Code Playgroud)
保存时,它会直接显示包含更新数据的表.在表格中显示它时,我使用DecimalFormat确保以适当的方式显示它:
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);
Run Code Online (Sandbox Code Playgroud)
这是它给我看的价值:8.765432E7
当我再次选择该行以查看EditText中的值时,我看到:87654320.00
价值如何变化?在其他情况下,我也将数据保存为"50009876.99",但不知何故它不保存.99并将其设为.00即50009876.00.
为什么事情不正常?我哪里错了?
任何帮助都非常感谢.
*编辑:用于在表格中显示的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);
}
Run Code Online (Sandbox Code Playgroud)
我认为你的问题是你将数字存储为Float.Float没有很好的精度-它实际上只有23的精度二进制数字所看到这里.这意味着您无法准确地存储在float小数点之前的7位数或更差 - 在该点之前的7位数和之后的几位数.这意味着您错误地选择了余额变量的类型 - 您无法存储在Float8 + 2中.我建议你改变类型Double,它具有更大的精度范围.
| 归档时间: |
|
| 查看次数: |
11086 次 |
| 最近记录: |