安卓/ SQLite的:实数类型保存/恢复数据 [英] Android/SQLite : Saving/Retrieving data of type REAL

查看:122
本文介绍了安卓/ SQLite的:实数类型保存/恢复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有表字段REAL的SQLDB。在我的布局我已经设置其字段的大小,只允许8 + 2位。在我的目标我已经使用的数据类型的字段为浮动。

  QS [0] =CREATE TABLE+ RELATION_TABLE_NAME +(ID文本PRIMARY KEY,name文本NOT NULL,startBal REAL NOT NULL,currentBal REAL NOT NULL);;

< EditText上的android:文本=机器人:ID =@ + ID / relCurrBalTxt的风格=@风格/ EditTextStyle
 机器人:inputType =numberDecimal机器人:最大长度=11机器人:提示=12345678.99/>
 

我在EDITTEXT输入值87654321.99,并点击保存。它填充物

  //发送数据到对象
rowData.setValue(2,Float.parseFloat(sbalTxt.getText()的toString()));
//在对象,这是我将它保存
this.setCurrentBalance(((浮点)值).floatValue()); //值是Object类型

在ContenteValues​​保存到数据库//保存数据
// LOG RCVD的对象,同时节省CurrBal:8.765432E7
values​​.put(currentBal,新的浮动(rowData.getValue(3)的ToString()));
 

在节能,它直接显示表更新后的数据。虽然显示其表我使用的DecimalFormat,以确保它显示在正确的方式:

  field_type = r.getFieldType(场);
//获取数据
海峡= r.getValue(场)的ToString();

//格式因此
如果(field_type.equals(DBRow.DOUBLE_TYPE)|| field_type.equals(DBRow.FLOAT_TYPE)){
        双douValue = Double.parseDouble(STR);
            // NumberFormat的NF = NumberFormat.getNumberInstance(Locale.ITALY);
            // DecimalFormat的DF =(DecimalFormat的)NF;
            DecimalFormat的DF =新的DecimalFormat();
            df.applyPattern(##,###,###。##。);
            df.setGroupingUsed(真正的);
            海峡= df.format(douValue);
        // Log.v(东风,虽然显示在表:双=+ douValue +字符串=+ STR);
        }

((TextView中)tr.getChildAt(场))的setText(STR)。
 

这表明我的价值:8.765432E7

当我再次选择该行看到我看到的EditText值:87654320.00

如何在值改变了吗?在另一些情况下还我保存的数据为50009876.99,但不知何故,不保存0.99,原本为.00,即50009876.00。

为什么东西都不能正常工作?我在哪里去了?

任何帮助是非常AP preciated。

*的编辑:DecimalFromat $用来表显示C $ C *

  //格式相应
    如果(field_type.equals(DBRow.DOUBLE_TYPE)|| field_type.equals(DBRow.FLOAT_TYPE)){
        / *
                      用这个code到现在
    的DecimalFormatSymbols DFS =新的DecimalFormatSymbols();
    串decep =将String.valueOf(dfs.getDecimalSeparator());
    INT指数= str.indexOf(decSep);
    如果(指数大于0){
       //如果小数点后的数字是超过2
       如果(str.substring(索引+1).length()→2){
        海峡= str.substring(0,索引+ 3);
    }
    }
    DecimalFormat的DF =新的DecimalFormat();
            df.setGroupingUsed(真正的);
            海峡= df.format(Double.parseDouble(STR));

            如果(addRow)
        create_addTextView(TR,STR,真正的);
    其他
        ((TextView中)tr.getChildAt(场))的setText(STR)。

            * /

                 // DecimalFormat的code
            双douValue = Double.parseDouble(STR);
            DecimalFormat的DF =新的DecimalFormat();
            df.applyPattern(##,###,###。##。);
            df.setGroupingUsed(真正的);
            海峡= df.format(douValue);

            如果(addRow)
       create_addTextView(TR,STR,真正的);
    其他
      ((TextView中)tr.getChildAt(场))的setText(STR)。

}
 

解决方案

我觉得你的问题是,你存储的数字作为浮动浮动没有很好precision - 它实际上有precision只有23所看到的二进制数字的此处。这意味着你不能准确地储存在浮动 7小数点或者更糟之前的数字 - 点和一对夫妇前,后7位。这意味着你选择了不正确的平衡变量的类型 - 你不能在浮动 8 + 2存储。我建议你​​改变类型双击,其中有显著更大范围precision。

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" />

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

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);

HERE it showed me the value : 8.765432E7

When I again selected that row to see the values in EditText I see : 87654320.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 ?

Any help is highly appreciated.

*EDIT : DecimalFromat code used to display in table *

        // 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);

}

解决方案

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.

这篇关于安卓/ SQLite的:实数类型保存/恢复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆