Android / SQLite:保存/检索REAL类型的数据

时间:2012-01-28 10:57:43

标签: android sqlite formatting floating-point

我有一个带有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" />

我在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()));

在保存时,它会直接显示包含更新数据的表格。在表格中显示时,我使用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);

这是它给我看的价值: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);

}

1 个答案:

答案 0 :(得分:5)

我认为您的问题是您将数字存储为FloatFloat没有很好的精度 - 它实际上只有23个精度的二进制数字here。这意味着您无法在小数点前的float 7位数内准确存储,甚至更糟糕 - 在该点之前的7位数和之后的几位数。这意味着您错误地选择了余额变量的类型 - 您无法存储在Float 8 + 2中。我建议你将类型更改为Double,它具有更大的精度范围。